-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.sv.bak
276 lines (245 loc) · 7.52 KB
/
cpu.sv.bak
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
module cpu (
input wire system_ready,
input wire clk_in,
input wire keyboard[15:0],
input wire [7:0] random_number,
output int cycle_counter,
output wire vram [0:1023],
output wire [7:0] sound_timer
);
logic [15:0] program_counter;
logic [7:0] memory[0:4095];
logic halt;
int watch_key;
logic [15:0] stack[0:15];
logic [15:0] index_reg;
logic [3:0] stack_pointer;
logic [7:0] registers[0:15];
logic [15:0] opcode;
logic [7:0] delay_timer;
logic [15:0] scratch;
logic [15:0] scratch2;
logic [7:0] scratch_8;
logic [7:0] scratch_82;
logic [31:0] x_cord;
logic [31:0] y_cord;
logic [7:0] size;
logic [7:0] sprite_pixel;
logic [7:0] i8;
initial begin
$readmemh("fontset.bin", memory, 0);
$readmemb("rom.bin", memory, 'h200);
end
task write_pixels;
input [31:0] x;
input [31:0] y;
int i;
begin
// bottom left
i = (y*64) + x;
vram[i] ^= 1;
end
endtask
always_ff @(negedge clk_in) begin
opcode = {memory[program_counter+0], memory[program_counter+1]};
if (cycle_counter % 20 == 0) begin
if (delay_timer > 0) delay_timer--;
if (sound_timer > 0) sound_timer--;
end
casez (opcode)
'h00E0: begin
for (int i = 0; i < 2048; i++) begin
vram[i] = 0;
end
end
'h00EE: begin
stack_pointer--;
program_counter = stack[stack_pointer];
end
'h1???: begin
program_counter = (opcode & 'h0FFF) - 2;
end
'h2???: begin
stack[stack_pointer] = program_counter;
stack_pointer++;
program_counter = (opcode & 'h0FFF) - 2;
end
'h3???: begin
scratch = (opcode & 'h00FF);
if (scratch[7:0] == registers[(opcode&'h0F00)>>8]) begin
program_counter += 2;
end
end
'h4???: begin
scratch = (opcode & 'h00FF);
if (scratch[7:0] != registers[(opcode&'h0F00)>>8]) begin
program_counter += 2;
end
end
'h5??0: begin
if (registers[(opcode&'h00F0)>>4] == registers[(opcode&'h0F00)>>8]) begin
program_counter += 2;
end
end
'h6???: begin
scratch = (opcode & 'h00FF);
registers[(opcode&'h0F00)>>8] = scratch[7:0];
end
'h7???: begin
scratch = (opcode & 'h00FF);
registers[(opcode&'h0F00)>>8] += scratch[7:0];
end
'h8??0: begin
registers[(opcode&'h0F00)>>8] = registers[(opcode&'h00F0)>>4];
end
'h8??1: begin
registers[(opcode&'h0F00)>>8] |= registers[(opcode&'h00F0)>>4];
registers[15] = 0;
end
'h8??2: begin
registers[(opcode&'h0F00)>>8] &= registers[(opcode&'h00F0)>>4];
registers[15] = 0;
end
'h8??3: begin
registers[(opcode&'h0F00)>>8] ^= registers[(opcode&'h00F0)>>4];
registers[15] = 0;
end
'h8??4: begin
scratch_8 = registers[(opcode&'h0F00)>>8];
registers[(opcode&'h0F00)>>8] += registers[(opcode&'h00F0)>>4];
registers[15] = {7'b0000000, scratch_8 > registers[(opcode&'h0F00)>>8]};
end
'h8??5: begin
scratch_8 = registers[(opcode&'h0F00)>>8];
registers[(opcode&'h0F00)>>8] -= registers[(opcode&'h00F0)>>4];
registers[15] = {7'b0000000, scratch_8 >= registers[(opcode&'h0F00)>>8]};
end
'h8??6: begin
scratch_8 = registers[(opcode&'h0F00)>>8];
registers[(opcode&'h0F00)>>8] = registers[(opcode&'h00F0)>>4] >> 1;
registers[15] = {7'b0000000, ((scratch_8 & 8'h01) == 8'h01)};
end
'h8??7: begin
scratch_8 = registers[(opcode&'h00F0)>>4];
scratch_82 = registers[(opcode&'h0F00)>>8];
registers[(opcode & 'h0F00) >> 8] = registers[(opcode & 'h00F0) >> 4] - registers[(opcode & 'h0F00) >> 8];
registers[15] = {7'b0000000, (scratch_8 >= scratch_82)};
end
'h8??E: begin
scratch_8 = registers[(opcode&'h0F00)>>8];
registers[(opcode&'h0F00)>>8] = registers[(opcode&'h00F0)>>4] << 1;
registers[15] = {7'b0000000, (scratch_8[7])};
end
'h9??0: begin
if (registers[(opcode&'h00F0)>>4] != registers[(opcode&'h0F00)>>8]) begin
program_counter += 2;
end
end
'hA???: begin
index_reg = (opcode & 'h0FFF);
end
'hb???: begin
program_counter = {8'h00, registers[0]} + (opcode & 'h0FFF) - 2;
end
'hc???: begin
// TODO: use a real RNG module, this is not synthesizeable
scratch = {8'h00, random_number} % 16'h0100;
scratch2 = (opcode & 'h00FF);
registers[(opcode&'h0F00)>>8] = scratch[7:0] & scratch2[7:0];
end
'hD???: begin
if (cycle_counter % 20 != 0) begin
halt = 1;
end else begin
halt = 0;
x_cord = {24'h000000, registers[(opcode&'h0F00)>>8]} % 64;
y_cord = {24'h000000, registers[(opcode&'h00F0)>>4]} % 32;
scratch = (opcode & 'h000F);
size = scratch[7:0];
registers[15] = 0;
for (int r = 0; r < size; r++) begin
for (int c = 0; c < 8; c++) begin
if (!(r + y_cord >= 32 || x_cord + c >= 64)) begin
sprite_pixel = memory[{16'h0000, index_reg}+r] & ('h80 >> c);
if (|sprite_pixel) begin
write_pixels(x_cord + c, r+y_cord);
end
end
end
end
end
end
'hE?9E: begin
scratch_8 = registers[(opcode&'h0F00)>>8];
if (keyboard[scratch_8[3:0]] == 1) begin
program_counter += 2;
end
end
'hE?A1: begin
scratch_8 = registers[(opcode&'h0F00)>>8];
if (keyboard[scratch_8[3:0]] != 1) begin
program_counter += 2;
end
end
'hF?07: begin
registers[(opcode&'h0F00)>>8] = delay_timer;
end
'hF?0A: begin
halt = 1;
for (int i = 0; i < 16; i++) begin
if (watch_key == 255) begin
if (keyboard[i]) begin
watch_key = i;
end
end else begin
if (!keyboard[watch_key]) begin
halt = 0;
watch_key = 255;
end
end
end
end
'hF?15: begin
delay_timer = registers[(opcode&'h0F00)>>8];
end
'hF?18: begin
sound_timer = registers[(opcode&'h0F00)>>8];
end
'hF?1E: begin
index_reg = index_reg + {8'h00, registers[(opcode&'h0F00)>>8]};
end
'hF?29: begin
index_reg = registers[(opcode&'h0F00)>>8] * 5;
end
'hF?33: begin
scratch = {8'h00, registers[(opcode&'h0F00)>>8]};
scratch2 = scratch % 10;
memory[index_reg+2] = scratch2[7:0];
scratch /= 10;
scratch2 = scratch % 10;
memory[index_reg+1] = scratch2[7:0];
scratch /= 10;
scratch2 = scratch % 10;
memory[index_reg+0] = scratch2[7:0];
end
'hF?55: begin
scratch = (opcode & 'h0F00) >> 8;
for (i8 = 0; i8 <= scratch[7:0]; i8++) begin
scratch2 = index_reg + {8'h00, i8};
memory[scratch2[11:0]] = registers[i8[3:0]];
end
index_reg++;
end
'hF?65: begin
scratch = (opcode & 'h0F00) >> 8;
for (i8 = 0; i8 <= scratch[7:0]; i8++) begin
scratch2 = index_reg + {8'h00, i8};
registers[i8[3:0]] = memory[scratch2[11:0]];
end
index_reg++;
end
endcase
if (!halt) program_counter += 2;
cycle_counter++;
end
endmodule