提交 af5eab61 编写于 作者: Z Zihao Yu

move basic device handling to verilator by DPI

* TODO: implement keyboard and vga
上级 0cba5964
......@@ -4,6 +4,8 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <stdint.h>
#include <assert.h>
#ifdef __cplusplus
# include <stdexcept>
......
#include "common.h"
#include <sys/time.h>
static struct timeval boot = {};
void device_init(void) {
gettimeofday(&boot, NULL);
}
uint32_t uptime(void) {
struct timeval t;
gettimeofday(&t, NULL);
int s = t.tv_sec - boot.tv_sec;
int us = t.tv_usec - boot.tv_usec;
if (us < 0) {
s --;
us += 1000000;
}
return s * 1000 + (us + 500) / 1000;
}
extern "C" void device_helper(
uint8_t req_wen, uint32_t req_addr, uint32_t req_wdata, uint32_t *resp_rdata) {
switch (req_addr) {
// read uartlite stat register
case 0x40600008:
// read uartlite ctrl register
case 0x4060000c: *resp_rdata = 0; break;
// write uartlite data register
case 0x40600004: if (req_wen) eprintf("%c", (uint8_t)req_wdata); break;
// read RTC
case 0x40700000: *resp_rdata = uptime(); break;
// read key
case 0x40900000: *resp_rdata = 0; break;
// read screen size
case 0x40800000: *resp_rdata = 0; break;
// write vga sync
case 0x40800004: *resp_rdata = 0; break;
default:
if (req_addr >= 0x40000000 && req_addr < 0x40400000 && req_wen) {
// write to vmem
}
else {
assert(0);
}
}
}
......@@ -90,10 +90,13 @@ int main(int argc, const char** argv) {
return emu.get_cycles();
};
struct timeval t0, t1;
gettimeofday(&t0, NULL);
extern void device_init(void);
device_init();
auto timeout = emu.execute();
gettimeofday(&t1, NULL);
extern uint32_t uptime(void);
uint32_t ms = uptime();
if (timeout) {
eprintf(ANSI_COLOR_RED "Timeout after %lld cycles\n" ANSI_COLOR_RESET, (long long)emu.get_max_cycles());
......@@ -101,13 +104,7 @@ int main(int argc, const char** argv) {
extern void display_trapinfo(void);
display_trapinfo();
int s = t1.tv_sec - t0.tv_sec;
int us = t1.tv_usec - t0.tv_usec;
if (us < 0) {
s --;
us += 1000000;
}
eprintf(ANSI_COLOR_BLUE "Host time spent: %ds %6dus\n" ANSI_COLOR_RESET, s, us);
eprintf(ANSI_COLOR_BLUE "Host time spent: %dms\n" ANSI_COLOR_RESET, ms);
}
return timeout;
......
......@@ -5,6 +5,17 @@ import chisel3.util._
import bus.simplebus.SimpleBus
class DeviceHelper extends BlackBox {
val io = IO(new Bundle {
val clk = Input(Clock())
val reqValid = Input(Bool())
val reqWen = Input(Bool())
val reqAddr = Input(UInt(32.W))
val reqWdata = Input(UInt(32.W))
val respRdata = Output(UInt(32.W))
})
}
class SimMMIO extends Module {
val io = IO(new Bundle {
val rw = Flipped(new SimpleBus)
......@@ -22,6 +33,18 @@ class SimMMIO extends Module {
io.mmioTrap.valid := false.B
io.mmioTrap.cmd := 0.U
val helper = Module(new DeviceHelper)
helper.io.clk := clock
helper.io.reqValid := io.rw.req.valid
helper.io.reqWen := wen
helper.io.reqAddr := io.rw.req.bits.addr
helper.io.reqWdata := io.rw.req.bits.wdata
io.rw.resp.bits.rdata := helper.io.respRdata
io.rw.req.ready := true.B
io.rw.resp.valid := io.rw.req.valid
/*
when (io.rw.req.valid) {
switch (io.rw.req.bits.addr) {
is (0x40600008.U) {
......@@ -67,10 +90,11 @@ class SimMMIO extends Module {
io.mmioTrap.cmd := 5.U
}
}
*/
io.rw.req.ready := true.B
io.rw.resp.bits.rdata := io.mmioTrap.rdata
io.rw.resp.valid := io.mmioTrap.valid
//io.rw.req.ready := true.B
//io.rw.resp.bits.rdata := io.mmioTrap.rdata
//io.rw.resp.valid := io.mmioTrap.valid
assert(!io.rw.req.valid || io.mmioTrap.valid, "bad addr = 0x%x", io.rw.req.bits.addr)
//assert(!io.rw.req.valid || io.mmioTrap.valid, "bad addr = 0x%x", io.rw.req.bits.addr)
}
import "DPI-C" function void device_helper
(
input bit req_wen,
input int req_addr,
input int req_wdata,
output int resp_rdata
);
module DeviceHelper(
input clk,
input reqValid,
input reqWen,
input [31:0] reqAddr,
input [31:0] reqWdata,
output [31:0] respRdata
);
always @(posedge clk) begin
if (reqValid) device_helper(reqWen, reqAddr, reqWdata, respRdata);
end
endmodule
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册