-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuxnMultiOLD.sv
More file actions
407 lines (396 loc) · 24.8 KB
/
uxnMultiOLD.sv
File metadata and controls
407 lines (396 loc) · 24.8 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
module uxnProcessor (
input logic clk,
input logic rst,
input logic [15:0] instruction, // For testing purposes
output logic [15:0] data_out
);
// Define CPU states
typedef enum logic [2:0] {
INIT,
FETCH,
// DECODE, // Currently this is peformed asynchronously
EXECUTE,
UPDATE
} cpuState;
localparam OPCODEWIDTH = 8;
localparam INSTRUCTIONWIDTH = 16;
// CPU Registers and Memory
logic [15:0] PC; // Program Counter
logic [INSTRUCTIONWIDTH -1:0] IR; // Instruction Register
logic [INSTRUCTIONWIDTH -1:0] memory [0:65535]; // Simplified memory
// Stack Registers
logic [7:0] SP; // Stack Pointer
logic [INSTRUCTIONWIDTH -OPCODEWIDTH -1:0] workingStack [255:0]; // Big Endian Working Stack(Moves positively)
logic [INSTRUCTIONWIDTH -OPCODEWIDTH -1:0] returnStack [255:0]; // Big Endian Return Stack(Moves positively)
// Control Signals and State
cpuState state, nextState;
// Opcode Definition
localparam [OPCODEWIDTH -1:0] BRK = 8'h00, INC = 8'h01, POP = 8'h02, NIP = 8'h03, SWP = 8'h04, ROT = 8'h05, DUP = 8'h06, OVR = 8'h07, EQU = 8'h08, NEQ = 8'h09, GTH = 8'h0a, LTH = 8'h0b, JMP = 8'h0c, JCN = 8'h0d, JSR = 8'h0e, STH = 8'h0f;
localparam [OPCODEWIDTH -1:0] LDZ = 8'h10, STZ = 8'h11, LDR = 8'h12, STR = 8'h13, LDA = 8'h14, STA = 8'h15, DEI = 8'h16, DEO = 8'h17, ADD = 8'h18, SUB = 8'h19, MUL = 8'h1a, DIV = 8'h1b, AND = 8'h1c, ORA = 8'h1d, EOR = 8'h1e, SFT = 8'h1f;
localparam [OPCODEWIDTH -1:0] JCI = 8'h20;
localparam [OPCODEWIDTH -1:0] JSI = 8'h60;
localparam [OPCODEWIDTH -1:0] LIT = 8'h80;
// EXECUTE registers
logic [4:0] ALUResult; // 5 bits for now
assign data_out = ALUResult;
// DECODE STAGE
assign opcode = IR[INSTRUCTIONWIDTH -1: OPCODEWIDTH];
assign immediate = IR[INSTRUCTIONWIDTH -OPCODEWIDTH -1:0];
assign mode = IR[15:INSTRUCTIONWIDTH -3 -1];
// Sequential block for state transitions and synchronous operations
always_ff @(posedge clk or posedge rst) begin
if (rst) begin
PC <= 0;
IR <= 0;
// memory <= 0;
SP <= 0;
// stack <= 0;
state <= INIT;
end else begin
state <= nextState;
case (state)
INIT: begin
PC <= 0;
SP <= 0;
end
FETCH: begin
// IR <= instruction;
IR <= memory[PC]; // Fetch instruction
end
// DECODE: begin
// end
EXECUTE: begin // ALU is handled in combinational logic
case (opcode)
BRK: begin // Ends the evalutation of the current vector. This opcode has no modes.
;
end
INC: begin // Increments the value at the top of the stack, by 1.
;
end
POP: begin // Removes the value at the top of the stack.
SP <= SP - 8'b1;
end
NIP: begin // Removes the second value from the stack. This is practical to convert a short into a byte.
SP <= SP - 8'b1;
end
SWP: begin // Exchanges the first and second values at the top of the stack.
;
end
ROT: begin // Rotates three values at the top of the stack, to the left, wrapping around.
;
end
DUP: begin // Duplicates the value at the top of the stack.
SP <= SP + 8'b1;
end
OVR: begin // Duplicates the second value at the top of the stack.
SP <= SP + 8'b1;
end
EQU: begin // Pushes 01 to the stack if the two values at the top of the stack are equal, 00 otherwise.
SP <= SP + 8'b1;
end
NEQ: begin // Pushes 01 to the stack if the two values at the top of the stack are not equal, 00 otherwise.
SP <= SP + 8'b1;
end
GTH: begin // Pushes 01 to the stack if the second value at the top of the stack is greater than the value at the top of the stack, 00 otherwise.
SP <= SP + 8'b1;
end
LTH: begin // Pushes 01 to the stack if the second value at the top of the stack is lesser than the value at the top of the stack, 00 otherwise.
SP <= SP + 8'b1;
end
JMP: begin // Moves the PC by a relative distance equal to the signed byte on the top of the stack, or to an absolute address in short mode.
SP <= SP - 8'b1;
end
JCN: begin // If the byte preceeding the address is not 00, moves the PC by a signed value equal to the byte on the top of the stack, or to an absolute address in short mode.
;
end
JSR: begin // Pushes the PC to the return-stack and moves the PC by a signed value equal to the byte on the top of the stack, or to an absolute address in short mode.
;
end
STH: begin // Moves the value at the top of the stack to the return stack. Note that with the r-mode, the stacks are exchanged and the value is moved from the return stack to the working stack.
SP <= SP + 8'b1;
end
LDZ: begin // Pushes the value at an address within the first 256 bytes of memory, to the top of the stack.
SP <= SP + 8'b1;
end
STZ: begin // Writes a value to an address within the first 256 bytes of memory.
;
end
LDR: begin // Pushes a value at a relative address in relation to the PC, within a range between -128 and +127 bytes, to the top of the stack.
SP <= SP + 8'b1;
end
STR: begin // Writes a value to a relative address in relation to the PC, within a range between -128 and +127 bytes.
;
end
LDA: begin // Pushes the value at a absolute address, to the top of the stack.
SP <= SP + 8'b1;
end
STA: begin // Writes a value to a absolute address.
;
end
DEI: begin // Pushes a value from the device page, to the top of the stack. The target device might capture the reading to trigger an I/O event.
SP <= SP + 8'b1;
end
DEO: begin // Writes a value to the device page. The target device might capture the writing to trigger an I/O event.
;
end
ADD: begin // Pushes the sum of the two values at the top of the stack.
SP <= SP - 8'b1;
end
SUB: begin // Pushes the difference of the first value minus the second, to the top of the stack.
SP <= SP - 8'b1;
end
MUL: begin // Pushes the product of the first and second values at the top of the stack.
SP <= SP - 8'b1;
end
DIV: begin // Pushes the quotient of the first value over the second, to the top of the stack. A division by zero pushes zero on the stack. The rounding direction is toward zero.
SP <= SP - 8'b1;
end
AND: begin // Pushes the result of the bitwise operation AND, to the top of the stack.
SP <= SP - 8'b1;
end
ORA: begin // Pushes the result of the bitwise operation OR, to the top of the stack.
SP <= SP - 8'b1;
end
EOR: begin // Pushes the result of the bitwise operation XOR, to the top of the stack.
SP <= SP - 8'b1;
end
SFT: begin // Shifts the bits of the second value of the stack to the left or right, depending on the control value at the top of the stack. The high nibble of the control value indicates how many bits to shift left, and the low nibble how many bits to shift right. The rightward shift is done first.
;
end
endcase
end
UPDATE: begin
case (opcode)
BRK: begin // Ends the evalutation of the current vector. This opcode has no modes.
;
end
INC: begin // Increments the value at the top of the stack, by 1.
workingStack[SP -1] <= ALUResult;
end
POP: begin // Removes the value at the top of the stack.
;
end
NIP: begin // Removes the second value from the stack. This is practical to convert a short into a byte.
workingStack[SP -1] <= workingStack[SP];
end
SWP: begin // Exchanges the first and second values at the top of the stack.
// Need to test if this is fine!!!
workingStack[SP -1] <= workingStack[SP -2];
workingStack[SP -2] <= workingStack[SP -1];
end
ROT: begin // Rotates three values at the top of the stack, to the left, wrapping around.
workingStack[SP -1] <= workingStack[SP -3];
workingStack[SP -2] <= workingStack[SP -1];
workingStack[SP -3] <= workingStack[SP -2];
end
DUP: begin // Duplicates the value at the top of the stack.
workingStack[SP -1] <= workingStack[SP -2];
end
OVR: begin // Duplicates the second value at the top of the stack.
workingStack[SP -1] <= workingStack[SP -3];
end
EQU: begin // Pushes 01 to the stack if the two values at the top of the stack are equal, 00 otherwise.
workingStack[SP -1] <= ALUResult;
end
NEQ: begin // Pushes 01 to the stack if the two values at the top of the stack are not equal, 00 otherwise.
workingStack[SP -1] <= ALUResult;
end
GTH: begin // Pushes 01 to the stack if the second value at the top of the stack is greater than the value at the top of the stack, 00 otherwise.
workingStack[SP -1] <= ALUResult;
end
LTH: begin // Pushes 01 to the stack if the second value at the top of the stack is lesser than the value at the top of the stack, 00 otherwise.
workingStack[SP -1] <= ALUResult;
end
JMP: begin // Moves the PC by a relative distance equal to the signed byte on the top of the stack, or to an absolute address in short mode.
PC <= ALUResult;
end
JCN: begin // If the byte preceeding the address is not 00, moves the PC by a signed value equal to the byte on the top of the stack, or to an absolute address in short mode.
;
end
JSR: begin // Pushes the PC to the return-stack and moves the PC by a signed value equal to the byte on the top of the stack, or to an absolute address in short mode.
;
end
STH: begin // Moves the value at the top of the stack to the return stack. Note that with the r-mode, the stacks are exchanged and the value is moved from the return stack to the working stack.
;
end
LDZ: begin // Pushes the value at an address within the first 256 bytes of memory, to the top of the stack.
// workingStack[SP -1] <= memory[];
;
end
STZ: begin // Writes a value to an address within the first 256 bytes of memory.
memory[] <= ;
end
LDR: begin // Pushes a value at a relative address in relation to the PC, within a range between -128 and +127 bytes, to the top of the stack.
workingStack[SP -1] <= memory[PC +];
end
STR: begin // Writes a value to a relative address in relation to the PC, within a range between -128 and +127 bytes.
// memory[PC +] <= ;
;
end
LDA: begin // Pushes the value at a absolute address, to the top of the stack.
workingStack[SP -1] <= ALUResult;
end
STA: begin // Writes a value to a absolute address.
// memory[] <= ;
;
end
DEI: begin // Pushes a value from the device page, to the top of the stack. The target device might capture the reading to trigger an I/O event.
;
end
DEO: begin // Writes a value to the device page. The target device might capture the writing to trigger an I/O event.
;
end
ADD: begin // Pushes the sum of the two values at the top of the stack.
workingStack[SP -1] <= ALUResult;
end
SUB: begin // Pushes the difference of the first value minus the second, to the top of the stack.
workingStack[SP -1] <= ALUResult;
end
MUL: begin // Pushes the product of the first and second values at the top of the stack.
workingStack[SP -1] <= ALUResult;
end
DIV: begin // Pushes the quotient of the first value over the second, to the top of the stack. A division by zero pushes zero on the stack. The rounding direction is toward zero.
workingStack[SP -1] <= ALUResult;
end
AND: begin // Pushes the result of the bitwise operation AND, to the top of the stack.
workingStack[SP -1] <= ALUResult;
end
ORA: begin // Pushes the result of the bitwise operation OR, to the top of the stack.
workingStack[SP -1] <= ALUResult;
end
EOR: begin // Pushes the result of the bitwise operation XOR, to the top of the stack.
workingStack[SP -1] <= ALUResult;
end
SFT: begin // Shifts the bits of the second value of the stack to the left or right, depending on the control value at the top of the stack. The high nibble of the control value indicates how many bits to shift left, and the low nibble how many bits to shift right. The rightward shift is done first.
workingStack[SP -1] <= ALUResult;
end
endcase
end
endcase
end
end
// Combinational block for determining the next state and executing operations
always_comb begin
nextState = state; // Default to stay in the current state
case (state)
INIT: begin
nextState = FETCH;
end
FETCH: begin
// nextState = DECODE;
nextState = EXECUTE;
end
// DECODE: begin
// nextState = EXECUTE;
// // mode = IR[15:INSTRUCTIONWIDTH -3 -1];
// // opcode = IR[INSTRUCTIONWIDTH -3 -1: INSTRUCTIONWIDTH -OPCODEWIDTH -1];
// // intermediate = IR[INSTRUCTIONWIDTH -OPCODEWIDTH -1:0];
// end
EXECUTE: begin // ALU operation happens in combinational logic
case (opcode)
BRK: begin // Ends the evalutation of the current vector. This opcode has no modes.
;
end
INC: begin // Increments the value at the top of the stack, by 1.
ALUResult = workingStack[SP-2] + 8'd1;
end
POP: begin // Removes the value at the top of the stack.
;
end
NIP: begin // Removes the second value from the stack. This is practical to convert a short into a byte.
;
end
SWP: begin // Exchanges the first and second values at the top of the stack.
;
end
ROT: begin // Rotates three values at the top of the stack, to the left, wrapping around.
;
end
DUP: begin // Duplicates the value at the top of the stack.
;
end
OVR: begin // Duplicates the second value at the top of the stack.
;
end
EQU: begin // Pushes 01 to the stack if the two values at the top of the stack are equal, 00 otherwise.
ALUResult = (workingStack[SP-1] == workingStack[SP-2]) ? 8'h01 : 8'h00;
end
NEQ: begin // Pushes 01 to the stack if the two values at the top of the stack are not equal, 00 otherwise.
ALUResult = (workingStack[SP-1] != workingStack[SP-2]) ? 8'h01 : 8'h00;
end
GTH: begin // Pushes 01 to the stack if the second value at the top of the stack is greater than the value at the top of the stack, 00 otherwise.
ALUResult = (workingStack[SP-2] > workingStack[SP-1]) ? 8'h01 : 8'h00;
end
LTH: begin // Pushes 01 to the stack if the second value at the top of the stack is lesser than the value at the top of the stack, 00 otherwise.
ALUResult = (workingStack[SP-2] < workingStack[SP-1]) ? 8'h01 : 8'h00;
end
JMP: begin // Moves the PC by a relative distance equal to the signed byte on the top of the stack, or to an absolute address in short mode.
ALUResult = PC + workingStack[SP-1];
end
JCN: begin // If the byte preceeding the address is not 00, moves the PC by a signed value equal to the byte on the top of the stack, or to an absolute address in short mode.
;
end
JSR: begin // Pushes the PC to the return-stack and moves the PC by a signed value equal to the byte on the top of the stack, or to an absolute address in short mode.
;
end
STH: begin // Moves the value at the top of the stack to the return stack. Note that with the r-mode, the stacks are exchanged and the value is moved from the return stack to the working stack.
;
end
LDZ: begin // Pushes the value at an address within the first 256 bytes of memory, to the top of the stack.
;
end
STZ: begin // Writes a value to an address within the first 256 bytes of memory.
;
end
LDR: begin // Pushes a value at a relative address in relation to the PC, within a range between -128 and +127 bytes, to the top of the stack.
;
end
STR: begin // Writes a value to a relative address in relation to the PC, within a range between -128 and +127 bytes.
;
end
LDA: begin // Pushes the value at a absolute address, to the top of the stack.
;
end
STA: begin // Writes a value to a absolute address.
;
end
DEI: begin // Pushes a value from the device page, to the top of the stack. The target device might capture the reading to trigger an I/O event.
;
end
DEO: begin // Writes a value to the device page. The target device might capture the writing to trigger an I/O event.
;
end
ADD: begin // Pushes the sum of the two values at the top of the stack.
ALUResult = workingStack[SP-1] + workingStack[SP-2];
end
SUB: begin // Pushes the difference of the first value minus the second, to the top of the stack.
ALUResult = workingStack[SP-1] - workingStack[SP-2];
end
MUL: begin // Pushes the product of the first and second values at the top of the stack.
ALUResult = workingStack[SP-1] * workingStack[SP-2];
end
DIV: begin // Pushes the quotient of the first value over the second, to the top of the stack. A division by zero pushes zero on the stack. The rounding direction is toward zero.
ALUResult = workingStack[SP-1] / workingStack[SP-2];
end
AND: begin // Pushes the result of the bitwise operation AND, to the top of the stack.
ALUResult = workingStack[SP-1] & workingStack[SP-2];
end
ORA: begin // Pushes the result of the bitwise operation OR, to the top of the stack.
ALUResult = workingStack[SP-1] | workingStack[SP-2];
end
EOR: begin // Pushes the result of the bitwise operation XOR, to the top of the stack.
ALUResult = workingStack[SP-1] ^ workingStack[SP-2];
end
SFT: begin // Shifts the bits of the second value of the stack to the left or right, depending on the control value at the top of the stack. The high nibble of the control value indicates how many bits to shift left, and the low nibble how many bits to shift right. The rightward shift is done first.
// Low nibble are the bits 0-3; high nibble are bits 4-7.
ALUResult = (workingStack[SP-2] >> workingStack[SP-1][3:0]) << workingStack[SP-1][7:4];
end
endcase
nextState = UPDATE;
end
UPDATE: begin
nextState = FETCH;
end
endcase
end
endmodule