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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
|
`default_nettype none
`include "messages.vh"
module stack_machine_new
(
/* Those 2 are supposed to be common for both wishbone interfaces */
input wire CLK_I,
input wire RST_I,
/* Instruction reading interface */
input wire I_ACK_I,
output reg [19:0] I_ADR_O,
input wire [15:0] I_DAT_I,
output reg [15:0] I_DAT_O, /* Not used, interface read-only */
output reg I_STB_O,
output reg I_CYC_O,
output reg I_WE_O, /* Always 0, interface read-only */
input wire I_STALL_I,
/* Data interface */
input wire D_ACK_I,
input wire D_ERR_I, /* We'll start using it soon */
output reg [20:0] D_ADR_O,
input wire [31:0] D_DAT_I,
output reg [31:0] D_DAT_O,
output reg [3:0] D_SEL_O,
output reg D_STB_O,
output reg D_CYC_O,
output reg D_WE_O,
input wire D_STALL_I,
/* non-wishbone */
output wire finished
);
/* TODO: get back to the good old habit of using wires for all ports */
always @* begin
if (CLK_I || !CLK_I) begin /* avoiding "found no sensitivities" warning */
I_DAT_O = 16'bx;
I_WE_O = 1'b0;
end
end
reg [20:0] pc;
reg [20:0] sp;
always @* begin /* pc and sp should always be word-aligned */
if (CLK_I || !CLK_I) begin
pc[0] = 0;
sp[0] = 0;
end
end
`define SET_PC(address) if (1) begin pc[20:1] <= (address) / 2; end else
`define SET_SP(address) if (1) begin sp[20:1] <= (address) / 2; end else
reg [31:0] r0;
reg [31:0] r1;
wire signed [31:0] r0s;
wire signed [31:0] r1s;
assign r0s = r0;
assign r1s = r1;
reg [31:0] im;
reg im_initialized;
parameter STEP_LOADING_INSTRUCTION = 1'b0;
parameter STEP_EXECUTING = 1'b1;
reg step;
reg first_execution_tick;
reg [15:0] instruction;
/* Results of instruction parsing */
/*
* This flag informs us, that this is the special instruction used solely
* for setting im (it uses 15-bit payload instead of 7-bit one)
*/
wire set_im;
assign set_im = instruction[15];
/*
* This flag informs us whether instruction uses immediate (all instructions
* that use it must contain a 7-bit payload)
*/
wire use_im;
assign use_im = instruction[14] && !set_im;
/* Payloads for both kinds of instructions, that modify im */
wire [6:0] short_payload;
assign short_payload = instruction[6:0];
wire [14:0] long_payload;
assign long_payload = instruction[14:0];
/* Sign-extending payload when setting im */
wire payload_msb;
assign payload_msb = set_im ? long_payload[14] : short_payload[6];
wire [31:0] sign_extended_payload;
assign sign_extended_payload = set_im ? {{17{payload_msb}}, long_payload} :
use_im ? {{25{payload_msb}}, short_payload} :
32'bx;
/* Shifting payload into im that was already partially initialized */
wire [31:0] im_shifted_payload;
assign im_shifted_payload = set_im ? {im[16:0], long_payload} :
use_im ? {im[24:0], short_payload} :
32'bx;
/*
* If im has already been partially initialized, we'll just shift our
* payload into it. Otherwise, we sign-extend our payload and put it in im.
*/
wire [31:0] im_effective;
assign im_effective = im_initialized ?
im_shifted_payload :
sign_extended_payload;
/* Upon instruction stack can grow, shrink or remain the same size */
wire stack_shrinks;
assign stack_shrinks = instruction[13] == 1'b1 && !set_im;
wire stack_shrinks_by_1;
assign stack_shrinks_by_1 = stack_shrinks && instruction[12] == 1'b1;
wire stack_shrinks_by_2;
assign stack_shrinks_by_2 = stack_shrinks && instruction[12] == 1'b0;
wire stack_grows;
assign stack_grows = instruction[13:12] == 2'b01 && !set_im;
wire stack_same_size;
assign stack_same_size = instruction[13:12] == 2'b00 || set_im;
/* If instruction[11:10] == 2'b11, we have some load or store */
wire store;
assign store = stack_shrinks && use_im && instruction[11:10] == 2'b11;
wire load;
assign load = (stack_grows || stack_same_size) && use_im &&
instruction[11:10] == 2'b11;
/*
* Loads and stores can use either im or r1+im (r0+im) as address. Obviously,
* a variant of load/store that uses r1 (r0), consumes one more operand.
*/
wire addressing_with_operand;
assign addressing_with_operand = (load && stack_same_size) ||
(store && stack_shrinks_by_2);
wire [20:0] address_operand;
assign address_operand = load ? r1[20:0] : r0[20:0];
wire [20:0] addr_to_use;
assign addr_to_use = addressing_with_operand ?
im_effective + address_operand : im_effective;
/*
* Those tell us, how many bytes are load'ed or store'd. We might also later
* use those flags with instructions (e.g. type promotion).
*/
wire byte_operation;
wire word_operation;
wire dword_operation;
wire qword_operation; /* We won't implement these in hw */
wire [3:0] instruction_select_mask;
assign byte_operation = instruction[9:8] == 2'b00;
assign word_operation = instruction[9:8] == 2'b01;
assign dword_operation = instruction[9:8] == 2'b10;
assign qword_operation = instruction[9:8] == 2'b11;
assign instruction_select_mask = byte_operation ? 4'b0001 :
word_operation ? 4'b0011 :
4'b1111;
/* Flag mainly meant for load instructions, but not exclusively */
wire sign_extend;
assign sign_extend = instruction[7];
wire loaded_value_sign;
assign loaded_value_sign = !sign_extend ? 0 :
byte_operation ? D_DAT_I[7] :
word_operation ? D_DAT_I[15] : 1'bx;
/* Instructions other than load and store go here */
/* Instructions, that do not change stack size */
wire instr_halt;
assign instr_halt = !set_im && !use_im && stack_same_size &&
instruction[11:0] == 12'd0;
wire instr_nop;
assign instr_nop = !set_im && !use_im && stack_same_size &&
instruction[11:0] == 12'd1;
wire instr_swap;
assign instr_swap = !set_im && !use_im && stack_same_size &&
instruction[11:0] == 12'd2;
wire instr_set_sp;
assign instr_set_sp = use_im && stack_same_size &&
instruction[11:7] == 5'd0;
wire instr_jump;
assign instr_jump = use_im && stack_same_size &&
instruction[11:7] == 5'd1;
wire instr_add_sp;
assign instr_add_sp = use_im && stack_same_size &&
instruction[11:7] == 5'd2;
/* Instructions, that grow stack */
wire instr_tee;
assign instr_tee = !set_im && !use_im && stack_grows &&
instruction[11:0] == 12'd0;
wire instr_get_frame;
assign instr_get_frame = !set_im && !use_im && stack_grows &&
instruction[11:0] == 12'd1;
wire instr_const;
assign instr_const = use_im && stack_grows &&
instruction[11:7] == 5'd0;
wire instr_call;
assign instr_call = use_im && stack_grows &&
instruction[11:7] == 5'd1;
/* Instructions, that shrink stack */
wire instr_add;
assign instr_add = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd0;
wire instr_sub;
assign instr_sub = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd1;
wire instr_udiv;
assign instr_udiv = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd2;
wire instr_mul;
assign instr_mul = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd3;
wire instr_drop;
assign instr_drop = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd4;
wire instr_eq;
assign instr_eq = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd7;
wire instr_lt;
assign instr_lt = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd8;
wire instr_ult;
assign instr_ult = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd9;
wire instr_le;
assign instr_le = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd10;
wire instr_ule;
assign instr_ule = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd11;
wire instr_gt;
assign instr_gt = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd12;
wire instr_ugt;
assign instr_ugt = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd13;
wire instr_ge;
assign instr_ge = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd14;
wire instr_uge;
assign instr_uge = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'd15;
wire instr_ret;
assign instr_ret = !set_im && !use_im && stack_shrinks_by_1 &&
instruction[11:0] == 12'b000010000000;
wire instr_cond_jump;
assign instr_cond_jump = use_im && stack_shrinks_by_1 &&
instruction[11:7] == 5'd1;
wire instr_cond_jump_n;
assign instr_cond_jump_n = use_im && stack_shrinks_by_1 &&
instruction[11:7] == 5'd2;
reg halt; /* Set once a halt instruction is encountered */
assign finished = halt;
/* module for division */
wire [31:0] div_quotient;
wire [31:0] div_remainder;
wire div_done;
div
#(
.WIDTH(32)
) div
(
.clock(CLK_I),
.start(step == STEP_EXECUTING && first_execution_tick),
.dividend(r0),
.divisor(r1),
.quotient(div_quotient),
.remainder(div_remainder),
.done(div_done)
);
reg arithmetic_uncompleted;
wire arithmetic_completes;
assign arithmetic_completes = instr_udiv ? div_done :
instr_halt ? 0 :
1;
always @*
I_ADR_O = pc / 2;
reg instruction_requested;
reg [31:0] stack_put_value;
reg load_store_unrequested;
reg [1:0] stack_transfer_unrequested;
wire data_request_happens;
wire [1:0] stack_transfer_request_happens;
assign data_request_happens = D_STB_O && !D_STALL_I;
assign stack_transfer_request_happens[0] = !load_store_unrequested &&
data_request_happens;
assign stack_transfer_request_happens[1] = !load_store_unrequested &&
!stack_transfer_unrequested[0] &&
data_request_happens;
reg load_store_uncompleted;
reg [1:0] stack_transfer_uncompleted;
wire data_command_completes;
wire [1:0] stack_transfer_completes;
assign data_command_completes = D_ACK_I && D_CYC_O;
assign stack_transfer_completes[0] = !load_store_uncompleted &&
data_command_completes;
assign stack_transfer_completes[1] = !load_store_uncompleted &&
!stack_transfer_uncompleted[0] &&
data_command_completes;
always @ (posedge CLK_I) begin
if (RST_I) begin
`SET_PC(0);
`SET_SP(21'h0FFFFC);
I_STB_O <= 0;
I_CYC_O <= 0;
step <= STEP_LOADING_INSTRUCTION;
instruction_requested <= 0;
stack_put_value <= 31'bx;
D_ADR_O <= 21'bx;
D_DAT_O <= 32'bx;
D_SEL_O <= 4'bx;
D_STB_O <= 0;
D_CYC_O <= 0;
D_WE_O <= 0;
halt <= 0;
end else begin // if (RST_I)
case (step)
STEP_LOADING_INSTRUCTION : begin
instruction <= I_DAT_I;
if (I_STB_O && !I_STALL_I)
instruction_requested <= 1;
I_STB_O <= !instruction_requested && !(I_STB_O && !I_STALL_I);
I_CYC_O <= 1;
if (I_CYC_O && I_ACK_I) begin
instruction_requested <= 0;
`SET_PC(pc + 2);
step <= STEP_EXECUTING;
I_CYC_O <= 0;
end
arithmetic_uncompleted <= 1;
first_execution_tick <= 1;
load_store_unrequested <= 0;
stack_transfer_unrequested <= 2'b0;
load_store_uncompleted <= 0;
stack_transfer_uncompleted <= 2'b0;
end // case: STEP_LOADING_INSTRUCTION
STEP_EXECUTING : begin
first_execution_tick <= 0;
if (arithmetic_completes)
arithmetic_uncompleted <= 0;
if (((stack_grows || stack_shrinks || load || store) &&
first_execution_tick) ||
(load_store_uncompleted &&
!data_command_completes) ||
(stack_transfer_uncompleted[1] &&
!stack_transfer_completes[1]) ||
(arithmetic_uncompleted &&
!arithmetic_completes)) begin
step <= STEP_EXECUTING; /* Remain where we are */
end else begin
step <= STEP_LOADING_INSTRUCTION;
I_STB_O <= 1;
I_CYC_O <= 1;
D_CYC_O <= 0;
end
if (first_execution_tick) begin
if (load || store) begin
load_store_unrequested <= 1;
load_store_uncompleted <= 1;
end
if (stack_shrinks_by_2) begin
stack_transfer_unrequested <= 2'b11;
stack_transfer_uncompleted <= 2'b11;
end else if (stack_grows || stack_shrinks) begin
stack_transfer_unrequested <= 2'b10;
stack_transfer_uncompleted <= 2'b10;
end
end
if (first_execution_tick) begin
if (load) begin
D_ADR_O <= addr_to_use;
D_DAT_O <= 32'bx;
D_SEL_O <= instruction_select_mask;
D_STB_O <= 1;
D_CYC_O <= 1;
D_WE_O <= 0;
end else if (store) begin
D_ADR_O <= addr_to_use;
D_DAT_O <= r1;
D_SEL_O <= instruction_select_mask;
D_STB_O <= 1;
D_CYC_O <= 1;
D_WE_O <= 1;
end else if (stack_shrinks) begin
`SET_SP(sp + 4);
D_ADR_O <= sp;
D_DAT_O <= 32'bx;
D_SEL_O <= 4'b1111;
D_STB_O <= 1;
D_CYC_O <= 1;
D_WE_O <= 0;
end else if (stack_grows) begin
`SET_SP(sp - 4);
D_ADR_O <= sp - 4;
D_DAT_O <= r0;
D_SEL_O <= 4'b1111;
D_STB_O <= 1;
D_CYC_O <= 1;
D_WE_O <= 1;
end
/*
* If we want to offload value to memory because of stack
* growth, we may need to wait for load or store to complete
* first. In such case we need to back up the stack value.
*/
stack_put_value <= r0;
end // if (first_execution_tick)
if (data_request_happens) begin
if (load_store_unrequested) begin
load_store_unrequested <= 0;
end else begin
stack_transfer_unrequested
<= {stack_transfer_unrequested[0], 1'b0};
end
if (stack_transfer_unrequested[0] ||
(load_store_unrequested &&
stack_transfer_unrequested[1])) begin
if (stack_shrinks) begin
`SET_SP(sp + 4);
D_ADR_O <= sp;
D_DAT_O <= 32'bx;
D_SEL_O <= 4'b1111;
D_STB_O <= 1;
D_WE_O <= 0;
end else /* if (stack_grows) */ begin
`SET_SP(sp - 4);
D_ADR_O <= sp - 4;
D_DAT_O <= stack_put_value;
D_SEL_O <= 4'b1111;
D_STB_O <= 1;
D_WE_O <= 1;
end
end else begin // if (stack_transfer_unrequested[0] ||...
D_ADR_O <= 21'bx;
D_DAT_O <= 32'bx;
D_SEL_O <= 4'bx;
D_STB_O <= 0;
D_WE_O <= 0;
end // else: !if(stack_transfer_unrequested[0] ||...
end // if (data_request_happens)
if (data_command_completes) begin
if (load_store_uncompleted) begin
load_store_uncompleted <= 0;
end else begin
stack_transfer_uncompleted
<= {stack_transfer_uncompleted[0], 1'b0};
end
if (!(load_store_uncompleted ||
stack_transfer_uncompleted[0]))
D_CYC_O <= 0;
end
if (stack_shrinks && stack_transfer_completes)
r0 <= D_DAT_I;
if (store)
r1 <= r0;
if (stack_grows && first_execution_tick)
r0 <= r1;
if (load && load_store_uncompleted) begin
if (byte_operation)
r1 <= {{24{loaded_value_sign}}, D_DAT_I[7:0]};
else if (word_operation)
r1 <= {{16{loaded_value_sign}}, D_DAT_I[15:0]};
else
r1 <= D_DAT_I;
end
if (!first_execution_tick && use_im)
im <= 32'bx;
im_initialized <= set_im;
if (set_im || use_im)
im <= im_effective;
else
im <= 32'bx;
/* Instructions, that do not change stack size */
if (instr_halt)
halt <= 1;
if (instr_nop)
r1 <= r1;
if (instr_swap)
{r0, r1} <= {r1, r0};
if (instr_set_sp)
`SET_SP(im_effective);
if (instr_add_sp)
`SET_SP(im_effective + sp);
if (instr_jump)
`SET_PC(im_effective);
/* Instructions, that grow stack */
if (instr_tee)
r1 <= r1;
if (instr_get_frame && first_execution_tick)
r1 <= sp;
if (instr_const && first_execution_tick)
r1 <= im_effective;
if (instr_call && first_execution_tick) begin
r1 <= pc;
`SET_PC(im_effective);
end
/* Instructions, that shrink stack */
if (instr_add && arithmetic_uncompleted)
r1 <= r0 + r1;
if (instr_sub && arithmetic_uncompleted)
r1 <= r0 - r1;
if (instr_udiv && arithmetic_uncompleted)
r1 <= div_quotient;
if (instr_mul && arithmetic_uncompleted)
r1 <= r0 * r1;
if (instr_drop && arithmetic_uncompleted)
r1 <= r0;
if ((instr_cond_jump || instr_cond_jump_n) &&
arithmetic_uncompleted) begin
r1 <= r0;
if ((r1 && instr_cond_jump) ||
(!r1 && instr_cond_jump_n))
`SET_PC(im_effective);
end
if (instr_eq && arithmetic_uncompleted)
r1 <= r0 == r1;
if (instr_lt && arithmetic_uncompleted)
r1 <= r0s < r1s;
if (instr_ult && arithmetic_uncompleted)
r1 <= r0 < r1;
if (instr_le && arithmetic_uncompleted)
r1 <= r0s <= r1s;
if (instr_ule && arithmetic_uncompleted)
r1 <= r0 <= r1;
if (instr_gt && arithmetic_uncompleted)
r1 <= r0s > r1s;
if (instr_ugt && arithmetic_uncompleted)
r1 <= r0 > r1;
if (instr_ge && arithmetic_uncompleted)
r1 <= r0s >= r1s;
if (instr_uge && arithmetic_uncompleted)
r1 <= r0 >= r1;
if (instr_ret && arithmetic_uncompleted) begin
r1 <= r0;
`SET_PC(r1);
end
if (first_execution_tick) begin
`DBG(("r0: %x r1: %x", r0, r1));
`DBG(("CPU: Executing %0s instruction",
store ? "store (kind?)" :
load ? "load (kind?)" :
instr_halt ? "halt" :
instr_nop ? "nop" :
instr_swap ? "swap" :
instr_set_sp ? "set_sp" :
instr_jump ? "jump" :
instr_add_sp ? "add_sp" :
instr_tee ? "tee" :
instr_get_frame ? "get_frame" :
instr_const ? "const" :
instr_call ? "call" :
instr_add ? "add" :
instr_sub ? "sub" :
instr_udiv ? "udiv" :
instr_mul ? "mul" :
instr_drop ? "drop" :
instr_eq ? "eq" :
instr_lt ? "lt" :
instr_ult ? "ult" :
instr_le ? "le" :
instr_ule ? "ule" :
instr_gt ? "gt" :
instr_ugt ? "ugt" :
instr_ge ? "ge" :
instr_uge ? "uge" :
instr_ret ? "ret" :
instr_cond_jump ? "cond_jump" :
instr_cond_jump_n ? "cond_jump_n" :
set_im ? "im" :
"unknown"));
end // if (first_execution_tick)
end // case: STEP_EXECUTING
endcase // case (step)
end // else: !if(RST_I)
end // always @ (posedge CLK_I)
`ifdef SIMULATION
/*
* RST should still be used when powering up, even in benches;
* this is just to avoid undefined values
*/
initial begin
I_ADR_O <= 0;
I_STB_O <= 0;
I_CYC_O <= 0;
D_ADR_O <= 0;
D_DAT_O <= 0;
D_STB_O <= 0;
D_CYC_O <= 0;
D_WE_O <= 0;
`SET_PC(0);
`SET_SP(0);
r0 <= 0;
r1 <= 0;
im <= 0;
im_initialized <= 0;
step <= 0;
first_execution_tick <= 0;
instruction <= 0;
halt <= 0;
instruction_requested <= 0;
stack_put_value <= 0;
load_store_unrequested <= 0;
stack_transfer_unrequested <= 2'b0;
load_store_uncompleted <= 0;
stack_transfer_uncompleted <= 2'b0;
end // initial begin
`endif
endmodule // stack_machine_new
|