SAIFIINDUSTRIES commited on
Commit
408933c
·
verified ·
1 Parent(s): d15f819

Add Repo: rgwan_kamikaze

Browse files
rgwan_kamikaze/README.md ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Kamikaze:
2
+
3
+ Light-weight RV32IMC core for FPGA or ASIC.
4
+
5
+ Description:
6
+ ---------
7
+
8
+ It realized a RISC-V 32bit core by 4-stage pipeline. It aims to replace Cortex-M3.
9
+
10
+ Currently is not usable, working in progress.
11
+
12
+
13
+ Goals:
14
+ ---------
15
+
16
+ RISC-V RV32IMC instruction set compatible.
17
+
18
+ I will add some instruction to enhance it's performance, if I have enough time working on it.
19
+
20
+ Harvard architecture which features I-bus and D-bus.
21
+
22
+ Small footprint for small FPGA or ASIC.
23
+
24
+ AHB-Lite or AXI bus wrapper.
25
+
26
+ Debug supported. The debug register will mapped on MMIO(APB interface), and support debug externally from special UART.
27
+
28
+ Authors:
29
+ ----------
30
+
31
+ Zhiyuan Wan
32
+
33
+ License:
34
+ ----------
35
+
36
+ MIT
37
+
rgwan_kamikaze/firmware/firmware.c ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #define GPIO_A_ODR (*(volatile char*)0x10000000)
2
+ void putc(char c)
3
+ {
4
+ int i;
5
+ volatile int j;
6
+ int ch = (c << 1) + (1 << 9);
7
+ for(i = 0; i < 10; i++) //
8
+ {
9
+ if(ch & (1 << i))
10
+ GPIO_A_ODR = 0x03;
11
+ else
12
+ GPIO_A_ODR = 0x00;
13
+ for(j = 0; j < 9; j++)
14
+ __asm__("nop");
15
+
16
+ }
17
+ }
18
+
19
+ void puts(const char *s)
20
+ {
21
+ volatile int j;
22
+ while (*s)
23
+ {
24
+ putc(*s++);
25
+ int j;
26
+ for(j = 0; j < 1000; j++)
27
+ __asm__("nop");
28
+ }
29
+ }
30
+
31
+
32
+ void *memcpy(void *dest, const void *src, int n)
33
+ {
34
+ while (n)
35
+ {
36
+ n--;
37
+ ((char*)dest)[n] = ((char*)src)[n];
38
+ }
39
+ return dest;
40
+ }
41
+
42
+ void main()
43
+ {
44
+ char message[] = "$Uryyb+Jbeyq!+Vs+lbh+pna+ernq+guvf+zrffntr+gura$gur+CvpbEI32+PCH"
45
+ "+frrzf+gb+or+jbexvat+whfg+svar.$$++++++++++++++++GRFG+CNFFRQ!$$";
46
+ for (int i = 0; message[i]; i++)
47
+ switch (message[i])
48
+ {
49
+ case 'a' ... 'm':
50
+ case 'A' ... 'M':
51
+ message[i] += 13;
52
+ break;
53
+ case 'n' ... 'z':
54
+ case 'N' ... 'Z':
55
+ message[i] -= 13;
56
+ break;
57
+ case '$':
58
+ message[i] = '\n';
59
+ break;
60
+ case '+':
61
+ message[i] = ' ';
62
+ break;
63
+ }
64
+ while(1)
65
+ {
66
+ /*putc(0x55);
67
+ putc(0xaa);*/
68
+ puts(message);
69
+
70
+ }
71
+ }
72
+
rgwan_kamikaze/firmware/makehex.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ #
3
+ # This is free and unencumbered software released into the public domain.
4
+ #
5
+ # Anyone is free to copy, modify, publish, use, compile, sell, or
6
+ # distribute this software, either in source code form or as a compiled
7
+ # binary, for any purpose, commercial or non-commercial, and by any
8
+ # means.
9
+
10
+ from sys import argv
11
+
12
+ binfile = argv[1]
13
+ nwords = int(argv[2])
14
+
15
+ with open(binfile, "rb") as f:
16
+ bindata = f.read()
17
+
18
+ assert len(bindata) < 4*nwords
19
+ assert len(bindata) % 4 == 0
20
+
21
+ for i in range(nwords):
22
+ if i < len(bindata) // 4:
23
+ w = bindata[4*i : 4*i+4]
24
+ print("%02x%02x%02x%02x" % (w[3], w[2], w[1], w[0]))
25
+ else:
26
+ print("0")
27
+
rgwan_kamikaze/firmware/rom2mif.c ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdio.h>
2
+ #include <stdint.h>
3
+
4
+ int main()
5
+ {
6
+ FILE *fp, *fp1, *fp2, *fp3, *fp4;
7
+ fp = fopen("firmware.bin", "rb");
8
+ fp1 = fopen("hi.mif", "w+");
9
+ fp2 = fopen("mh.mif", "w+");
10
+ fp3 = fopen("ml.mif", "w+");
11
+ fp4 = fopen("lo.mif", "w+");
12
+ if(!fp)
13
+ {
14
+ printf("File doesn't exist!\n");
15
+ return -1;
16
+ }
17
+ fseek(fp, 0, SEEK_END);
18
+ int size = ftell(fp);
19
+ fseek(fp, 0, SEEK_SET);
20
+ printf("Binary length: %d bytes\n", size);
21
+
22
+ uint8_t dat[4];
23
+ int i = 0;
24
+ fprintf(fp1, "DEPTH = 1024;\nWIDTH = 8;\n"
25
+ "ADDRESS_RADIX = HEX;\n"
26
+ "DATA_RADIX = HEX;\n"
27
+ "CONTENT "
28
+ "BEGIN\n \n");
29
+ fprintf(fp2, "DEPTH = 1024;\nWIDTH = 8;\n"
30
+ "ADDRESS_RADIX = HEX;\n"
31
+ "DATA_RADIX = HEX;\n"
32
+ "CONTENT "
33
+ "BEGIN\n \n");
34
+ fprintf(fp3, "DEPTH = 1024;\nWIDTH = 8;\n"
35
+ "ADDRESS_RADIX = HEX;\n"
36
+ "DATA_RADIX = HEX;\n"
37
+ "CONTENT "
38
+ "BEGIN\n \n");
39
+ fprintf(fp4, "DEPTH = 1024;\nWIDTH = 8;\n"
40
+ "ADDRESS_RADIX = HEX;\n"
41
+ "DATA_RADIX = HEX;\n"
42
+ "CONTENT "
43
+ "BEGIN\n \n");
44
+ while(!feof(fp))
45
+ {
46
+ fread(&dat, 1, 4, fp);
47
+ fprintf(fp1, "%04X : %02X;\n", i, dat[3]);
48
+ fprintf(fp2, "%04X : %02X;\n", i, dat[2]);
49
+ fprintf(fp3, "%04X : %02X;\n", i, dat[1]);
50
+ fprintf(fp4, "%04X : %02X;\n", i, dat[0]);
51
+ i++;
52
+ }
53
+ fprintf(fp1, "\nEND;\n");
54
+ fprintf(fp2, "\nEND;\n");
55
+ fprintf(fp3, "\nEND;\n");
56
+ fprintf(fp4, "\nEND;\n");
57
+ fclose(fp);
58
+ fclose(fp1);
59
+ fclose(fp2);
60
+ fclose(fp3);
61
+ fclose(fp4);
62
+ printf("Done\n");
63
+ }
rgwan_kamikaze/firmware/testserial.c ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdio.h>
2
+
3
+ void tx(char c)
4
+ {
5
+ int i;
6
+ volatile int j;
7
+ int ch = (c << 1) + (1 << 9);
8
+ for(i = 0; i < 10; i++) //
9
+ {
10
+ if(ch & (1 << i))
11
+ printf("1");
12
+ else
13
+ printf("0");
14
+ }
15
+ }
16
+
17
+ main()
18
+ {
19
+ int i;
20
+ for(i = 0; i < 8; i++)
21
+ {
22
+ tx(1 << i);//
23
+ puts("");
24
+ }
25
+ }
rgwan_kamikaze/scripts/kamikaze_test/config.v ADDED
@@ -0,0 +1 @@
 
 
1
+ `define FPGA_ARCH_ANLOGIC_AL3
rgwan_kamikaze/src/compress_decoder.v ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2015 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the “License”); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ /* It is taken from RI5CY project to kamikaze. Thanks them! */
11
+ ////////////////////////////////////////////////////////////////////////////////
12
+ // Engineer: Sven Stucki - svstucki@student.ethz.ch //
13
+ // //
14
+ // Design Name: Compressed instruction decoder //
15
+ // Project Name: RI5CY //
16
+ // Language: SystemVerilog //
17
+ // //
18
+ // Description: Decodes RISC-V compressed instructions into their RV32 //
19
+ // equivalent. This module is fully combinatorial. //
20
+ // //
21
+ ////////////////////////////////////////////////////////////////////////////////
22
+
23
+
24
+ `include "riscv_defines.v"
25
+
26
+ module kamikaze_compress_decoder
27
+ (
28
+ input [31:0] instr_i,
29
+ output reg [31:0] instr_o,
30
+ output reg illegal_instr_o
31
+ );
32
+
33
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
34
+ // ____ _ ____ _ //
35
+ // / ___|___ _ __ ___ _ __ _ __ ___ ___ ___ ___ __| | | _ \ ___ ___ ___ __| | ___ _ __ //
36
+ // | | / _ \| '_ ` _ \| '_ \| '__/ _ \/ __/ __|/ _ \/ _` | | | | |/ _ \/ __/ _ \ / _` |/ _ \ '__| //
37
+ // | |__| (_) | | | | | | |_) | | | __/\__ \__ \ __/ (_| | | |_| | __/ (_| (_) | (_| | __/ | //
38
+ // \____\___/|_| |_| |_| .__/|_| \___||___/___/\___|\__,_| |____/ \___|\___\___/ \__,_|\___|_| //
39
+ // |_| //
40
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
41
+
42
+ always @*
43
+ begin
44
+ illegal_instr_o = 1'b0;
45
+ instr_o = 32'bx;
46
+
47
+ case (instr_i[1:0])
48
+ // C0
49
+ 2'b00: begin
50
+ case (instr_i[15:13])
51
+ 3'b000: begin
52
+ // c.addi4spn -> addi rd', x2, imm
53
+ instr_o = {2'b0, instr_i[10:7], instr_i[12:11], instr_i[5], instr_i[6], 2'b00, 5'h02, 3'b000, 2'b01, instr_i[4:2], `OPC_OP_IMM};
54
+ if (instr_i[12:5] == 8'b0) illegal_instr_o = 1'b1;
55
+ end
56
+
57
+ 3'b010: begin
58
+ // c.lw -> lw rd', imm(rs1')
59
+ instr_o = {5'b0, instr_i[5], instr_i[12:10], instr_i[6], 2'b00, 2'b01, instr_i[9:7], 3'b010, 2'b01, instr_i[4:2], `OPC_LOAD};
60
+ end
61
+
62
+ 3'b110: begin
63
+ // c.sw -> sw rs2', imm(rs1')
64
+ instr_o = {5'b0, instr_i[5], instr_i[12], 2'b01, instr_i[4:2], 2'b01, instr_i[9:7], 3'b010, instr_i[11:10], instr_i[6], 2'b00, `OPC_STORE};
65
+ end
66
+
67
+ default: begin
68
+ illegal_instr_o = 1'b1;
69
+ end
70
+ endcase
71
+ end
72
+
73
+ // C1
74
+ 2'b01: begin
75
+ case (instr_i[15:13])
76
+ 3'b000: begin
77
+ // c.addi -> addi rd, rd, nzimm
78
+ // c.nop
79
+ instr_o = {{6 {instr_i[12]}}, instr_i[12], instr_i[6:2], instr_i[11:7], 3'b0, instr_i[11:7], `OPC_OP_IMM};
80
+ end
81
+
82
+ 3'b001, 3'b101: begin
83
+ // 001: c.jal -> jal x1, imm
84
+ // 101: c.j -> jal x0, imm
85
+ instr_o = {instr_i[12], instr_i[8], instr_i[10:9], instr_i[6], instr_i[7], instr_i[2], instr_i[11], instr_i[5:3], {9 {instr_i[12]}}, 4'b0, ~instr_i[15], `OPC_JAL};
86
+ end
87
+
88
+ 3'b010: begin
89
+ // c.li -> addi rd, x0, nzimm
90
+ instr_o = {{6 {instr_i[12]}}, instr_i[12], instr_i[6:2], 5'b0, 3'b0, instr_i[11:7], `OPC_OP_IMM};
91
+ if (instr_i[11:7] == 5'b0) illegal_instr_o = 1'b1;
92
+ end
93
+
94
+ 3'b011: begin
95
+ // c.lui -> lui rd, imm
96
+ instr_o = {{15 {instr_i[12]}}, instr_i[6:2], instr_i[11:7], `OPC_LUI};
97
+
98
+ if (instr_i[11:7] == 5'h02) begin
99
+ // c.addi16sp -> addi x2, x2, nzimm
100
+ instr_o = {{3 {instr_i[12]}}, instr_i[4:3], instr_i[5], instr_i[2], instr_i[6], 4'b0, 5'h02, 3'b000, 5'h02, `OPC_OP_IMM};
101
+ end else if (instr_i[11:7] == 5'b0) begin
102
+ illegal_instr_o = 1'b1;
103
+ end
104
+
105
+ if ({instr_i[12], instr_i[6:2]} == 6'b0) illegal_instr_o = 1'b1;
106
+ end
107
+
108
+ 3'b100: begin
109
+ case (instr_i[11:10])
110
+ 2'b00,
111
+ 2'b01: begin
112
+ // 00: c.srli -> srli rd, rd, shamt
113
+ // 01: c.srai -> srai rd, rd, shamt
114
+ instr_o = {1'b0, instr_i[10], 5'b0, instr_i[6:2], 2'b01, instr_i[9:7], 3'b101, 2'b01, instr_i[9:7], `OPC_OP_IMM};
115
+ if (instr_i[12] == 1'b1) illegal_instr_o = 1'b1;
116
+ if (instr_i[6:2] == 5'b0) illegal_instr_o = 1'b1;
117
+ end
118
+
119
+ 2'b10: begin
120
+ // c.andi -> andi rd, rd, imm
121
+ instr_o = {{6 {instr_i[12]}}, instr_i[12], instr_i[6:2], 2'b01, instr_i[9:7], 3'b111, 2'b01, instr_i[9:7], `OPC_OP_IMM};
122
+ end
123
+
124
+ 2'b11: begin
125
+ case ({instr_i[12], instr_i[6:5]})
126
+ 3'b000: begin
127
+ // c.sub -> sub rd', rd', rs2'
128
+ instr_o = {2'b01, 5'b0, 2'b01, instr_i[4:2], 2'b01, instr_i[9:7], 3'b000, 2'b01, instr_i[9:7], `OPC_OP};
129
+ end
130
+
131
+ 3'b001: begin
132
+ // c.xor -> xor rd', rd', rs2'
133
+ instr_o = {7'b0, 2'b01, instr_i[4:2], 2'b01, instr_i[9:7], 3'b100, 2'b01, instr_i[9:7], `OPC_OP};
134
+ end
135
+
136
+ 3'b010: begin
137
+ // c.or -> or rd', rd', rs2'
138
+ instr_o = {7'b0, 2'b01, instr_i[4:2], 2'b01, instr_i[9:7], 3'b110, 2'b01, instr_i[9:7], `OPC_OP};
139
+ end
140
+
141
+ 3'b011: begin
142
+ // c.and -> and rd', rd', rs2'
143
+ instr_o = {7'b0, 2'b01, instr_i[4:2], 2'b01, instr_i[9:7], 3'b111, 2'b01, instr_i[9:7], `OPC_OP};
144
+ end
145
+
146
+ 3'b100,
147
+ 3'b101,
148
+ 3'b110,
149
+ 3'b111: begin
150
+ // 100: c.subw
151
+ // 101: c.addw
152
+ illegal_instr_o = 1'b1;
153
+ end
154
+ endcase
155
+ end
156
+ endcase
157
+ end
158
+
159
+ 3'b110, 3'b111: begin
160
+ // 0: c.beqz -> beq rs1', x0, imm
161
+ // 1: c.bnez -> bne rs1', x0, imm
162
+ instr_o = {{4 {instr_i[12]}}, instr_i[6:5], instr_i[2], 5'b0, 2'b01, instr_i[9:7], 2'b00, instr_i[13], instr_i[11:10], instr_i[4:3], instr_i[12], `OPC_BRANCH};
163
+ end
164
+
165
+ default: begin
166
+ illegal_instr_o = 1'b1;
167
+ end
168
+ endcase
169
+ end
170
+
171
+ // C2
172
+ 2'b10: begin
173
+ case (instr_i[15:13])
174
+ 3'b000: begin
175
+ // c.slli -> slli rd, rd, shamt
176
+ instr_o = {7'b0, instr_i[6:2], instr_i[11:7], 3'b001, instr_i[11:7], `OPC_OP_IMM};
177
+ if (instr_i[11:7] == 5'b0) illegal_instr_o = 1'b1;
178
+ if (instr_i[12] == 1'b1 || instr_i[6:2] == 5'b0) illegal_instr_o = 1'b1;
179
+ end
180
+
181
+ 3'b010: begin
182
+ // c.lwsp -> lw rd, imm(x2)
183
+ instr_o = {4'b0, instr_i[3:2], instr_i[12], instr_i[6:4], 2'b00, 5'h02, 3'b010, instr_i[11:7], `OPC_LOAD};
184
+ if (instr_i[11:7] == 5'b0) illegal_instr_o = 1'b1;
185
+ end
186
+
187
+ 3'b100: begin
188
+ if (instr_i[12] == 1'b0) begin
189
+ // c.mv -> add rd/rs1, x0, rs2
190
+ instr_o = {7'b0, instr_i[6:2], 5'b0, 3'b0, instr_i[11:7], `OPC_OP};
191
+
192
+ if (instr_i[6:2] == 5'b0) begin
193
+ // c.jr -> jalr x0, rd/rs1, 0
194
+ instr_o = {12'b0, instr_i[11:7], 3'b0, 5'b0, `OPC_JALR};
195
+ end
196
+ end else begin
197
+ // c.add -> add rd, rd, rs2
198
+ instr_o = {7'b0, instr_i[6:2], instr_i[11:7], 3'b0, instr_i[11:7], `OPC_OP};
199
+
200
+ if (instr_i[11:7] == 5'b0) begin
201
+ // c.ebreak -> ebreak
202
+ instr_o = {32'h00_10_00_73};
203
+ if (instr_i[6:2] != 5'b0)
204
+ illegal_instr_o = 1'b1;
205
+ end else if (instr_i[6:2] == 5'b0) begin
206
+ // c.jalr -> jalr x1, rs1, 0
207
+ instr_o = {12'b0, instr_i[11:7], 3'b000, 5'b00001, `OPC_JALR};
208
+ end
209
+ end
210
+ end
211
+
212
+ 3'b110: begin
213
+ // c.swsp -> sw rs2, imm(x2)
214
+ instr_o = {4'b0, instr_i[8:7], instr_i[12], instr_i[6:2], 5'h02, 3'b010, instr_i[11:9], 2'b00, `OPC_STORE};
215
+ end
216
+
217
+ default: begin
218
+ illegal_instr_o = 1'b1;
219
+ end
220
+ endcase
221
+ end
222
+
223
+ default: begin
224
+ // 32 bit (or more) instruction
225
+ instr_o = instr_i;
226
+ end
227
+ endcase
228
+ end
229
+
230
+ endmodule
rgwan_kamikaze/src/csr.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ 0x300 MSTATUS: IE[0], PRIV[2:1], CY[3]
2
+ 0x341 MEPC Storage last pc and jump to interrupt or exception address
3
+ 0x342 MCAUSE INT[31](it is trigged by an interrupt), Exception op-code[4:0]
4
+ 0x301 MISA ISA support RV32IMC
5
+
6
+ 0xF11 MVID "ANLG" (Anlogic)
7
+ 0xF12 MARCHID "ZYKZ" (Zhiyuan Kamikaze)
8
+ 0xF13 MIMPID "0000" (r0p0)
9
+
10
+ /* performance counter will not be implemented in this CPU */
rgwan_kamikaze/src/decode.v ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `include "riscv_defines.v"
2
+
3
+ /* 解码和寄存器读取 */
4
+ module kamikaze_decode( clk_i,
5
+ rst_i,
6
+ is_compressed_instr_i,
7
+ instr_i,
8
+ instr_valid_i,
9
+ pc_i,
10
+
11
+ rf_rs1_o,
12
+ rf_rs2_o,
13
+
14
+ rf_rs1_i,
15
+ rf_rs2_i,
16
+
17
+ rf_rd_o,
18
+ rf_rd_we_o,
19
+
20
+ decode_valid_o,
21
+
22
+ alu_func_o,
23
+
24
+ alu_op1_o,
25
+ alu_op2_o,
26
+
27
+ ex_we_i,
28
+ ex_wd_i,
29
+ ex_wdata_i,
30
+
31
+
32
+ pc_o,
33
+ pc_next_o
34
+
35
+ );
36
+
37
+ input clk_i;
38
+ input rst_i;
39
+
40
+ input is_compressed_instr_i;
41
+ input [31:0] instr_i;
42
+ input instr_valid_i;
43
+
44
+ input [31:0] pc_i; /*虽然没什么卵用*/
45
+ input [31:0] pc_next_i; /*下一个pc,分支预测是否正确*/
46
+
47
+ /* 寄存器地址输出 */
48
+ output reg [4:0] rf_rs1_o;
49
+ output reg [4:0] rf_rs2_o;
50
+
51
+ /* 寄存器数据输入 */
52
+ input [31:0] rf_rs1_i;
53
+ input [31:0] rf_rs2_i;
54
+
55
+ /* ALU 输出 */
56
+ output reg [31:0] alu_op1_o;
57
+ output reg [31:0] alu_op2_o;
58
+
59
+ /* 到回写阶段的 */
60
+ output reg [4:0] rf_rd_o = 0; /* 寄存器 2R 1W地址口, 同步RAM?分布RAM? */
61
+ output reg rf_rd_we_o;
62
+
63
+ output reg [2:0] alu_func_o = 0;
64
+
65
+ /* 从回写阶段判断是否有写的数据 */
66
+ input ex_we_i;
67
+ input [4:0]ex_wd_i;
68
+ input [31:0]ex_wdata_i;
69
+
70
+ output reg [31:0] pc_o;
71
+ output wire [31:0] pc_next_o;
72
+
73
+ assign pc_next_o = pc_i;
74
+
75
+ output reg decode_valid_o; /* 指令是否有效 */
76
+
77
+
78
+ reg is_compressed_instr = 0;
79
+
80
+ /* 指令解码 */
81
+
82
+ wire [6:0] opcode = instr_i[6:0];
83
+
84
+ /* 寄存器 */
85
+ wire [4:0] rs1_i = instr_i[19:15];
86
+ wire [4:0] rs2_i = instr_i[24:20];
87
+ wire [4:0] rd_i = instr_i[11:7];
88
+
89
+ /* 立即数 */
90
+ wire [31:0] d_imm_i = { {21{ instr_i[31] }}, instr_i[30:25], instr_i[24:21], instr_i[20]};
91
+ wire [31:0] d_imm_s = { {21{ instr_i[31] }}, instr_i[30:25], instr_i[11:8], instr_i[7]};
92
+ wire [31:0] d_imm_b = { {20{ instr_i[31] }}, instr_i[7], instr_i[30:25], instr_i[11:8], 1'b0};
93
+
94
+ wire [31:0] d_imm_u = { instr_i[31], instr_i[30:20], instr_i[19:12], 12'h000};
95
+
96
+ wire [31:0] d_imm_j = { {12{instr_i[31]}}, instr_i[19:12], instr_i[20],
97
+ instr_i[30:25], instr_i[24:21], 1'b0};
98
+
99
+
100
+ wire [2:0] alu_func = instr_i[14:12];
101
+
102
+ wire [31:0]rf_rs1;
103
+ wire [31:0]rf_rs2;
104
+
105
+
106
+ /* 流水线结果前递 */
107
+ assign rf_rs1 = ex_we_i? (ex_wd_i == rs1_i? ex_wdata_i: rf_rs1_i): rf_rs1_i;
108
+ assign rf_rs2 = ex_we_i? (ex_wd_i == rs2_i? ex_wdata_i: rf_rs2_i): rf_rs2_i;
109
+
110
+ always @(posedge clk_i or negedge rst_i)
111
+ begin
112
+ if(!rst_i)
113
+ begin
114
+ decode_valid_o <= 0;
115
+ rf_rd_we_o <= 0;
116
+ pc_o <= 0;
117
+ end
118
+ else
119
+ begin
120
+ rf_rs1_o <= rs1_i; /* 可能可以提前 */
121
+ rf_rs2_o <= rs2_i;
122
+ rf_rd_o <= rd_i; /* 寄存器解码 */
123
+
124
+ decode_valid_o <= instr_valid_i;
125
+
126
+ alu_func_o <= 0;
127
+
128
+ rf_rd_we_o <= 0;
129
+
130
+ case (opcode)
131
+ `OPC_OP_IMM:
132
+ begin
133
+ alu_func_o <= alu_func;
134
+ alu_op2_o <= d_imm_i;
135
+
136
+ alu_op1_o <= rf_rs1;
137
+
138
+ rf_rd_we_o <= instr_valid_i;
139
+ end
140
+ /*OPC_LUI:
141
+ OPC_AUIPC:
142
+ OPC_OP:
143
+ OPC_JAL:
144
+ OPC_JALR:
145
+ OPC_BRANCH:
146
+ OPC_LOAD:
147
+ OPC_STORE:
148
+ OPC_SYSTEM:*/
149
+ default:
150
+ begin
151
+ decode_valid_o <= 0;
152
+ end
153
+ endcase
154
+
155
+ pc_o <= pc_i;
156
+ is_compressed_instr <= is_compressed_instr_i;
157
+ end
158
+ end
159
+
160
+
161
+ endmodule
rgwan_kamikaze/src/execute.v ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* 执行和内存访问 */
2
+ `include "riscv_defines.v"
3
+
4
+ module kamikaze_execute(clk_i,
5
+ rst_i,
6
+
7
+ alu_op1_i,
8
+ alu_op2_i,
9
+ alu_func_i,
10
+
11
+ pc_i,
12
+ pc_o,
13
+
14
+ /* 到回写阶段 */
15
+ rf_rd_i,
16
+ rf_rd_o,
17
+ rf_rd_we_i,
18
+ rf_rd_we_o,
19
+
20
+ /* 输出 */
21
+ result_o
22
+ );
23
+ input clk_i;
24
+ input rst_i;
25
+
26
+ input [31:0]alu_op1_i;/* ALU输入1 */
27
+ input [31:0]alu_op2_i; /* ALU输入2 */
28
+
29
+ input [2:0]alu_func_i; /* ALU 功能选择 */
30
+ output reg [31:0] result_o; /* ALU 输出 */
31
+
32
+
33
+ /* 到回写阶段 */
34
+ input [4:0]rf_rd_i;
35
+ output reg [4:0]rf_rd_o;
36
+
37
+ input rf_rd_we_i;
38
+ output reg rf_rd_we_o;
39
+
40
+
41
+ input [31:0]pc_i;
42
+ output [31:0] pc_o;
43
+
44
+ reg [31:0] alu_out;
45
+
46
+ reg [31:0] pc;
47
+
48
+
49
+
50
+ assign pc_o = pc;
51
+
52
+
53
+ always @(posedge clk_i or negedge rst_i)
54
+ begin
55
+ if(!rst_i)
56
+ begin
57
+ pc <= 0;
58
+ end
59
+ else
60
+ begin
61
+ //pc <= pc_i;
62
+ /*result_o <= alu_out;
63
+
64
+ rf_rd_o <= rf_rd_i;
65
+ rf_rd_we_o <= rf_rd_we_i;*/
66
+ end
67
+ end
68
+
69
+ always @*
70
+ begin
71
+ result_o <= alu_out;
72
+
73
+ rf_rd_o <= rf_rd_i;
74
+ rf_rd_we_o <= rf_rd_we_i;
75
+ end
76
+
77
+ /* ALU */
78
+
79
+ /* 加法器和减法器 */
80
+
81
+ /* ALU选择逻辑 */
82
+ always @*
83
+ begin
84
+ case (alu_func_i)
85
+ `ALU_ADD:
86
+ alu_out <= alu_op1_i + alu_op2_i;
87
+ `ALU_XOR:
88
+ alu_out <= alu_op1_i ^ alu_op2_i;
89
+ `ALU_OR:
90
+ alu_out <= alu_op1_i | alu_op2_i;
91
+ `ALU_AND:
92
+ alu_out <= alu_op1_i & alu_op2_i;
93
+ default:
94
+ alu_out <= 32'bx;
95
+ endcase
96
+
97
+ end
98
+
99
+
100
+
101
+
102
+ endmodule
rgwan_kamikaze/src/fetch.v ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns/1ps
2
+
3
+ module kamikaze_fetch(clk_i,
4
+ rst_i,
5
+
6
+ im_addr_o,
7
+ im_data_i,
8
+
9
+ instr_o,
10
+ instr_valid_o,
11
+ is_compressed_instr_o,
12
+
13
+ pc_o);
14
+
15
+ input clk_i;
16
+ input rst_i;
17
+ input [31:0] im_data_i;
18
+ output [31:0] instr_o;
19
+ output [31:0] im_addr_o;
20
+ output instr_valid_o;
21
+ output is_compressed_instr_o;
22
+ output [31:0] pc_o;
23
+ output [31:0] pc_next_o;
24
+ //input stall_i; /* IF 停止信号 */
25
+
26
+ wire [30:0] word_address = im_addr_o[31:2];
27
+ reg stall_i = 0;
28
+
29
+ reg [31:0] pc;
30
+ reg [31:0] pc_4;
31
+ reg [2:0] pc_add;
32
+ reg [2:0] pc_add_prev;
33
+
34
+
35
+ reg [31:0] last_instr; /* 一级缓冲 */
36
+ reg is_compressed_instr;
37
+ reg fetch_start;
38
+
39
+ assign is_compressed_instr_o = is_compressed_instr;
40
+
41
+ reg align_wait; /* 对齐等待 */
42
+
43
+ assign instr_valid_o = !align_wait && !illegal_instr_c && fetch_start;
44
+ localparam CPU_START = 32'h0; /* 启动地址 */
45
+
46
+ assign im_addr_o = pc_4[1]? (pc_4 + 2'b10): pc_4; /* 舍入 */
47
+ assign stall_requiring = (pc_add_prev == 2) && (pc[1:0] == 2'b00); /* 16位对齐等待,防止冲数据 */
48
+
49
+ assign pc_o = pc;
50
+
51
+ reg [31:0]instr_t; /* 临时存放 */
52
+
53
+ wire illegal_instr_c;/* 错误的压缩指令 */
54
+
55
+ always @(posedge clk_i or negedge rst_i)
56
+ begin
57
+ if(!rst_i)
58
+ begin
59
+ pc_4 <= {CPU_START[31:2], 2'b00};
60
+ pc <= {CPU_START[31:2], 2'b00};/* PC 比 pc_4 滞后1 CLK */
61
+ fetch_start <= 0;
62
+ pc_add_prev <= 4;
63
+ last_instr <= 32'h0;
64
+ align_wait = CPU_START[1];
65
+ end
66
+ else
67
+ begin
68
+ if(!stall_i)
69
+ begin
70
+ if(fetch_start == 1'b0)
71
+ begin
72
+ fetch_start <= 1'b1; /* 取 0 指令 */
73
+ pc_4 <= pc_4 + 16'h4;
74
+ end
75
+ else
76
+ begin
77
+ if(align_wait)
78
+ align_wait <= 0;
79
+
80
+ pc_4 <= pc_4 + pc_add;
81
+ pc <= pc + pc_add;
82
+
83
+ if(!stall_requiring)
84
+ last_instr <= im_data_i;
85
+
86
+ pc_add_prev <= pc_add;
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ always @*
93
+ begin
94
+ if(pc[1:0] == 2'b00)
95
+ begin
96
+ if(stall_requiring)
97
+ begin
98
+ if(last_instr[1:0] != 2'b11) /* 对齐的压缩指令 */
99
+ begin
100
+ is_compressed_instr <= 1;
101
+ instr_t = last_instr[15:0];
102
+ end
103
+ else
104
+ begin
105
+ is_compressed_instr <= 0;
106
+ instr_t = last_instr[31:0];
107
+ end
108
+ end
109
+ else
110
+ begin
111
+ if(im_data_i[1:0] != 2'b11) /* 对齐的压缩指令 */
112
+ begin
113
+ is_compressed_instr <= 1;
114
+ instr_t = im_data_i[15:0];
115
+ end
116
+ else
117
+ begin
118
+ is_compressed_instr <= 0;
119
+ instr_t = im_data_i[31:0];
120
+ end
121
+ end
122
+ end
123
+ else
124
+ begin //pc[1:0] == 10
125
+ if(last_instr[17:16] != 2'b11) /* 不对齐的压缩指令 */
126
+ begin
127
+ is_compressed_instr <= 1;
128
+ instr_t = last_instr[31:16];
129
+ end
130
+ else /* 不对齐的非压缩指令 */
131
+ begin
132
+ is_compressed_instr <= 0;
133
+ instr_t = {im_data_i[15:0], last_instr[31:16]};
134
+ end
135
+ end
136
+
137
+ pc_add = is_compressed_instr? 2: 4;
138
+ end
139
+
140
+ kamikaze_compress_decoder c_dec(.instr_i(instr_t), .instr_o(instr_o), .illegal_instr_o(illegal_instr_c));
141
+ /* 解码压缩指令 */
142
+
143
+ /* 这地方得有个分支预测器,你说我是直接预测全部不跳好,还是预测全部跳好呢? */
144
+
145
+ endmodule
rgwan_kamikaze/src/int_exp.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ interrupt & exception table:
2
+ base_address: IVT_BASE, must be aligned to 4
3
+
4
+ initial_sp
5
+ reset_address
6
+ bus_error(misaligned/invalid memory access)
7
+ execute_error(divided by zero or illegal instruction)
8
+ sys_call(ecall instruction, ebreak will triggers the debugger if it is online)
9
+ nmi(not maskable interrupt, it is interrupt 0)
10
+ interrupts 1-31
11
+
rgwan_kamikaze/src/kamikaze.v ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* 神疯RISC-V RV32IMC CPU 顶层文件 */
2
+ /* Zhiyuan Wan,Jan. 2017 */
3
+ module kamikaze(clk_i,
4
+ rst_i,
5
+ im_addr_o,
6
+ im_data_i);
7
+ input clk_i;
8
+ input rst_i;
9
+ output [31:0] im_addr_o;
10
+ input [31:0] im_data_i;
11
+
12
+ wire [31:0] instruction;
13
+ wire is_compressed_instr;
14
+ wire instr_valid;
15
+
16
+ wire [31:0] pc_fetch;
17
+
18
+ /* 寄存器 */
19
+ wire [4:0] reg_raddr_1;
20
+ wire [4:0] reg_raddr_2;
21
+
22
+ wire [31:0] reg_rdata_1;
23
+ wire [31:0] reg_rdata_2;
24
+
25
+ wire [4:0] reg_waddr;
26
+ wire [31:0] reg_wdata;
27
+ wire reg_we;
28
+
29
+ /* 解码后 */
30
+ wire [2:0] alu_func;
31
+
32
+ wire [4:0]decoded_rd;
33
+ wire [31:0]decoded_imm;
34
+ wire [31:0]decoded_pc;
35
+ wire [31:0]decoded_pc_next;
36
+
37
+ wire [31:0]alu_op1;
38
+ wire [31:0]alu_op2;
39
+
40
+ wire decoded_rd_we;
41
+ wire executed_rd_we;
42
+
43
+ wire [4:0]executed_rd;
44
+ wire [31:0]executed_rd_data;
45
+
46
+ assign reg_we = executed_rd_we;
47
+ assign reg_wdata = executed_rd_data;
48
+ assign reg_waddr = executed_rd;
49
+
50
+
51
+ kamikaze_fetch fetch( .clk_i(clk_i), .rst_i(rst_i), /* 取指,扩展 */
52
+ .im_addr_o(im_addr_o), .im_data_i(im_data_i),
53
+ .instr_o(instruction), .is_compressed_instr_o(is_compressed_instr),
54
+ .instr_valid_o(instr_valid), .pc_o(pc_fetch)
55
+ );
56
+
57
+ kamikaze_decode decode( .clk_i(clk_i), .rst_i(rst_i), /* 解码,读寄存器 */
58
+ .instr_i(instruction), .is_compressed_instr_i(is_compressed_instr),
59
+
60
+ .pc_i(pc_fetch), .instr_valid_i(instr_valid),
61
+
62
+ .alu_func_o(alu_func),
63
+
64
+ .rf_rs1_o(reg_raddr_1), .rf_rs2_o(reg_raddr_2),
65
+ .rf_rs1_i(reg_rdata_1), .rf_rs2_i(reg_rdata_2), /* 访问寄存器 */
66
+
67
+ .alu_op1_o(alu_op1), .alu_op2_o(alu_op2),
68
+
69
+ .rf_rd_o(decoded_rd), .rf_rd_we_o(decoded_rd_we),
70
+
71
+ .ex_we_i(reg_we), .ex_wd_i(reg_waddr), .ex_wdata_i(reg_wdata),
72
+
73
+ .pc_o(decoded_pc), .pc_next_o(decoded_pc_next)
74
+ );
75
+
76
+ kamikaze_execute execute(.clk_i(clk_i), .rst_i(rst_i), /* 执行 */
77
+
78
+ .alu_op1_i(alu_op1), .alu_op2_i(alu_op2),
79
+
80
+ .alu_func_i(alu_func),
81
+
82
+ .pc_i(decoded_pc),
83
+
84
+ .rf_rd_i(decoded_rd),
85
+ .rf_rd_we_i(decoded_rd_we), .rf_rd_we_o(executed_rd_we),
86
+ .rf_rd_o(executed_rd), .result_o(executed_rd_data)
87
+ );
88
+
89
+ kamikaze_regfile regfile(.clk_i(clk_i), /* 写寄存器 */
90
+ .raddr1_i(reg_raddr_1), .raddr2_i(reg_raddr_2),
91
+ .rdata1_o(reg_rdata_1), .rdata2_o(reg_rdata_2),
92
+ .we_i(reg_we), .wdata_i(reg_wdata), .waddr_i(reg_waddr));
93
+
94
+ endmodule
rgwan_kamikaze/src/lsu.v ADDED
@@ -0,0 +1 @@
 
 
1
+ /* Load-Storage Unit,数据存储器访问器。用于访存的程序访问内存和读向量表。*/
rgwan_kamikaze/src/pipeline.txt ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ IF- Fetch and pre-decode compressed instruction to plain instruction. It is possible to add a 32bit-wide prefetcher to realize branch prediction, or we can simply don't use BP.
2
+
3
+ ID- Decode plain instruction to ALU function, memory access....control signal
4
+
5
+ EX1/MEM- Calculating address, and/or/xor/add stage. Expanding sign. Half multiply stage. Access the memory.
6
+ Align process will provided by memory controller, which is not inside the core.(? Maybe I will work around)
7
+
8
+ EX2/WB - Expanding sign from memory read, write result back to register file.
9
+
10
+ simple memory interface:
11
+
12
+ IM:
13
+
14
+ im_addr_o => instruction memory address, aligned by 4
15
+ im_data_i => instruction memory data
16
+ im_data_ready_i => wait state for instruction memory
17
+ im_data_req_o => instruction memory access signal, low means inactive.
18
+
19
+
20
+
21
+
rgwan_kamikaze/src/regfile.v ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ module kamikaze_regfile (clk_i,
2
+
3
+ waddr_i,
4
+ wdata_i,
5
+ we_i,
6
+
7
+ raddr1_i,
8
+ rdata1_o,
9
+
10
+ raddr2_i,
11
+ rdata2_o);
12
+ input clk_i;
13
+
14
+
15
+ input [4:0] waddr_i;
16
+ input [31:0] wdata_i;
17
+ input we_i;
18
+
19
+ input [4:0] raddr1_i;
20
+ input [4:0] raddr2_i;
21
+
22
+ output reg [31:0] rdata1_o;
23
+ output reg [31:0] rdata2_o;
24
+
25
+ /* 可能可以加倍时钟速度换取非双口的RAM实现,2R 1W */
26
+
27
+ reg [31:0]memory[0:31];
28
+
29
+ wire [31:0]cpureg_1 = memory[1];
30
+ wire [31:0]cpureg_2 = memory[2];
31
+ wire [31:0]cpureg_3 = memory[3];
32
+ wire [31:0]cpureg_4 = memory[4];
33
+ wire [31:0]cpureg_5 = memory[5];
34
+ wire [31:0]cpureg_6 = memory[6];
35
+ wire [31:0]cpureg_7 = memory[7];
36
+ wire [31:0]cpureg_8 = memory[8];
37
+ wire [31:0]cpureg_9 = memory[9];
38
+ wire [31:0]cpureg_10 = memory[10];
39
+ wire [31:0]cpureg_11 = memory[11];
40
+ wire [31:0]cpureg_12 = memory[12];
41
+ wire [31:0]cpureg_13 = memory[13];
42
+ wire [31:0]cpureg_14 = memory[14];
43
+ wire [31:0]cpureg_15 = memory[15];
44
+ wire [31:0]cpureg_16 = memory[16];
45
+ wire [31:0]cpureg_17 = memory[17];
46
+ wire [31:0]cpureg_18 = memory[18];
47
+ wire [31:0]cpureg_19 = memory[19];
48
+ wire [31:0]cpureg_20 = memory[20];
49
+ wire [31:0]cpureg_21 = memory[21];
50
+ wire [31:0]cpureg_22 = memory[22];
51
+ wire [31:0]cpureg_23 = memory[23];
52
+ wire [31:0]cpureg_24 = memory[24];
53
+ wire [31:0]cpureg_25 = memory[25];
54
+ wire [31:0]cpureg_26 = memory[26];
55
+ wire [31:0]cpureg_27 = memory[27];
56
+ wire [31:0]cpureg_28 = memory[28];
57
+ wire [31:0]cpureg_29 = memory[29];
58
+ wire [31:0]cpureg_30 = memory[30];
59
+ wire [31:0]cpureg_31 = memory[31];
60
+
61
+ integer i;
62
+ initial
63
+ for(i = 0; i < 32; i++)
64
+ memory[i] <= 32'h0;
65
+
66
+ always @(posedge clk_i)
67
+ begin
68
+ if(we_i)
69
+ memory[waddr_i] <= wdata_i;
70
+ end
71
+
72
+ always @*
73
+ begin
74
+ rdata1_o = raddr1_i? memory[raddr1_i]: 0;
75
+ rdata2_o = raddr2_i? memory[raddr2_i]: 0;
76
+ end
77
+ endmodule
rgwan_kamikaze/src/riscv_defines.v ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* RISC-V的编码常量 */
2
+
3
+ `define OPC_SYSTEM 7'h73
4
+ `define OPC_FENCE 7'h0f
5
+ `define OPC_OP 7'h33
6
+ `define OPC_OP_IMM 7'h13
7
+ `define OPC_STORE 7'h23
8
+ `define OPC_LOAD 7'h03
9
+ `define OPC_BRANCH 7'h63
10
+ `define OPC_JALR 7'h67
11
+ `define OPC_JAL 7'h6f
12
+ `define OPC_AUIPC 7'h17
13
+ `define OPC_LUI 7'h37
14
+
15
+ `define ALU_ADD 3'b000
16
+ `define ALU_SLTI 3'b010
17
+ `define ALU_SLTIU 3'b011
18
+ `define ALU_XOR 3'b100
19
+ `define ALU_OR 3'b110
20
+ `define ALU_AND 3'b111
21
+
rgwan_kamikaze/src/tb.v ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ `timescale 1ns/1ps
2
+
3
+ module system_tb;
4
+ reg rst = 0;
5
+ reg clk = 0;
6
+ initial begin
7
+ $dumpfile("kamikaze.vcd");
8
+ $dumpvars(0, system_tb);
9
+ #15 rst = 1;
10
+ #1000 $finish;
11
+
12
+ end
13
+ always begin
14
+ #5 clk = !clk;
15
+ end
16
+
17
+ top dut(.clk_i(clk), .rst_i(rst));
18
+
19
+ endmodule
rgwan_kamikaze/src/top.v ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ module top(clk_i, rst_i, io_o, trap);
2
+ input clk_i;
3
+ input rst_i;
4
+ output [7:0] io_o;
5
+ output trap;
6
+
7
+ `ifndef FPGA_ARCH_ANLOGIC_AL3
8
+ localparam MEM_SIZE = 4096;
9
+ reg [31:0] memory [0:MEM_SIZE-1];
10
+ initial $readmemh("../firmware/firmware.hex", memory);
11
+ `endif
12
+
13
+ wire [31:0] im_addr;
14
+ reg [31:0] im_data;
15
+
16
+ kamikaze cpu(.clk_i(clk_i),
17
+ .rst_i(rst_i),
18
+ .im_addr_o(im_addr),
19
+ .im_data_i(im_data));
20
+
21
+ `ifndef FPGA_ARCH_ANLOGIC_AL3
22
+ always @(posedge clk_i)
23
+ begin
24
+ if(rst_i)
25
+ im_data <= memory[im_addr[31:2]];
26
+ else
27
+ im_data <= 32'h0;
28
+ end
29
+ `else
30
+ assign io_o = im_data[7:0] ^ im_data[15:7] ^ im_data[23:16] ^ im_data[31:24];
31
+ `endif
32
+
33
+ endmodule
rgwan_kamikaze/src/writeback.v ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ /* 写回寄存器与访存 */
2
+ module kamikaze_writeback(clk_i,
3
+ rst_i);
4
+ input clk_i;
5
+ input rst_i;
6
+
7
+
8
+ endmodule