-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtestbench.sv
155 lines (130 loc) · 3.53 KB
/
testbench.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* NERV -- Naive Educational RISC-V Processor
*
* Copyright (C) 2020 N. Engelhardt <nak@yosyshq.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
module testbench;
localparam MEM_ADDR_WIDTH = 16;
localparam TIMEOUT = (1<<10);
reg clock;
reg reset = 1'b1;
reg stall = 1'b0;
wire trap;
wire [31:0] imem_addr;
reg [31:0] imem_data;
wire dmem_valid;
wire [31:0] dmem_addr;
wire [ 3:0] dmem_wstrb;
wire [31:0] dmem_wdata;
reg [31:0] dmem_rdata;
reg [31:0] irq = 'b0;
always #5 clock = clock === 1'b0;
always @(posedge clock) reset <= 0;
reg [7:0] mem [0:(1<<MEM_ADDR_WIDTH)-1];
wire wr_in_mem_range = (dmem_addr[31:2] < (1<<MEM_ADDR_WIDTH));
wire wr_in_output = (dmem_addr == 32'h 02000000);
reg [31:0] out;
reg out_valid;
always @(posedge clock) begin
if (out_valid) begin
$write("%c", out[7:0]);
`ifndef VERILATOR
$fflush();
`endif
end
end
`ifdef STALL
always @(posedge clock) begin
stall <= $random;
end
`endif
always @(posedge clock) begin
if (imem_addr >= (1<<MEM_ADDR_WIDTH)) begin
$display("Memory access out of range: imem_addr = 0x%08x", imem_addr);
end
if (dmem_valid && !(wr_in_mem_range || wr_in_output)) begin
$display("Memory access out of range: dmem_addr = 0x%08x", dmem_addr);
end
end
integer i;
always @(posedge clock) begin
out <= 32'h 0;
out_valid <= 1'b0;
if (!stall && !reset) begin
imem_data <= {
mem[{imem_addr[MEM_ADDR_WIDTH-1:2], 2'b11}],
mem[{imem_addr[MEM_ADDR_WIDTH-1:2], 2'b10}],
mem[{imem_addr[MEM_ADDR_WIDTH-1:2], 2'b01}],
mem[{imem_addr[MEM_ADDR_WIDTH-1:2], 2'b00}]
};
if (dmem_valid) begin
dmem_rdata <= {
mem[{dmem_addr[MEM_ADDR_WIDTH-1:2], 2'b11}],
mem[{dmem_addr[MEM_ADDR_WIDTH-1:2], 2'b10}],
mem[{dmem_addr[MEM_ADDR_WIDTH-1:2], 2'b01}],
mem[{dmem_addr[MEM_ADDR_WIDTH-1:2], 2'b00}]
};
for (i=0;i<4;i=i+1) begin
if (dmem_wstrb[i]) begin
if (wr_in_mem_range) begin
mem[{dmem_addr[MEM_ADDR_WIDTH-1:2], i[1:0]}] <= dmem_wdata[(i*8)+: 8];
end
if (wr_in_output) begin
out[(i*8)+: 8] <= dmem_wdata[(i*8)+: 8];
out_valid <= 1'b1;
end
dmem_rdata <= 'hx;
end
end
end else begin
dmem_rdata <= 32'h XXXX_XXXX;
end
end
end
initial begin
$readmemh("firmware.hex", mem);
if ($test$plusargs("vcd")) begin
$dumpfile("testbench.vcd");
$dumpvars(0, testbench);
end
end
nerv dut (
.clock(clock),
.reset(reset),
.stall(stall),
.trap(trap),
.imem_addr(imem_addr),
.imem_data(stall ? 32'bx : imem_data),
.dmem_valid(dmem_valid),
.dmem_addr(dmem_addr),
.dmem_wstrb(dmem_wstrb),
.dmem_wdata(dmem_wdata),
.dmem_rdata(stall ? 32'bx : dmem_rdata),
`ifdef NERV_FAULT
.imem_fault(1'b0),
.dmem_fault(1'b0),
`endif
.irq(irq)
);
reg [31:0] cycles = 0;
always @(posedge clock) begin
cycles <= cycles + 32'h1;
if (trap || (cycles >= TIMEOUT)) begin
$display("Simulated %0d cycles", cycles);
$finish;
end
end
endmodule