-
Notifications
You must be signed in to change notification settings - Fork 0
/
lc4.c
executable file
·1313 lines (1162 loc) · 28.2 KB
/
lc4.c
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CIS 240 HW 10: LC4 Simulator
* lc4.c
*/
#include "lc4.h"
#define INSN_OP(I) ((I) >> 12)
#define INSN_11_9(I) (((I) >> 9) & 0x7)
#define INSN_7_0(I) ((I) & 0xFF)
#define INSN_5(I) (((I) >> 5) & 0x1)
#define INSN_5_3(I) (((I) >> 3) & 0x7)
#define INSN_8_7(I) (((I) >> 7) & 0x3)
#define INSN_11(I) (((I) >> 11) & 0x1)
#define INSN_5_4(I) (((I) >> 4) & 0x3)
#define INSN_2_0(I) ((I) & 0x7)
#define INSN_8_6(I) (((I) >> 6) & 0x7)
#define INSN_8_0(I) ((I) & 0x1FF)
#define INSN_4_0(I) ((I) & 0x1F)
#define INSN_6_0(I) ((I) & 0x7F)
#define INSN_10_0(I) ((I) & 0x3FF)
#define INSN_5_0(I) ((I) & 0x3F)
#define INSN_3_0(I) ((I) & 0xF)
#define INSN_11_0(I) ((I) & 0x7FF)
FILE* file = 0;
/*
* Resets all control signals to 0.
* Param: pointer to control signals struct
*/
void clear_control_signals(ctrl* control) {
mux_ctl* mux = &control->mux_ctrls;
mux->pc_mux_ctl = 0;
mux->rs_mux_ctl = 0;
mux->rt_mux_ctl = 0;
mux->rd_mux_ctl = 0;
mux->reg_input_mux_ctl = 0;
mux->arith_mux_ctl = 0;
mux->logic_mux_ctl = 0;
mux->alu_mux_ctl = 0;
alu_ctrl* alu = &control->alu_ctrls;
alu->arith_ctl = 0;
alu->logic_ctl = 0;
alu->shift_ctl = 0;
alu->const_ctl = 0;
alu->cmp_ctl = 0;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 0;
reg->nzp_we = 0;
reg->data_we = 0;
}
/*
* Resets the machine state as PennSim would do.
* Param: pointer to current machine state
* Make sure to reset the entire lc4 machine, including the control signals!
*/
void reset_lc4(lc4_state* state, ctrl* control) {
state->PC = 33280;
state->PSR = 32770;
state->uimm = 0;
state->imm = 0;
state->rs_addr = 0;
state->rt_addr = 0;
state->rd_addr = 0;
total_memory* mem = &state->memory;
for (int i = 0; i < 8; i++) {
mem->R[i] = 0;
}
clear_control_signals(control);
}
void decode_br(ctrl* control, unsigned short int I) {
mux_ctl* mux = &control->mux_ctrls;
mux->pc_mux_ctl = 0;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 0;
reg->nzp_we = 0;
reg->data_we = 0;
int sub_op = INSN_11_9(I);
if (sub_op == 0) {
mux->pc_mux_ctl = 1;
}
}
void decode_arith(ctrl* control, unsigned short int I) {
int sub_op_imm = INSN_5(I);
mux_ctl* mux = &control->mux_ctrls;
mux->pc_mux_ctl = 1;
mux->rs_mux_ctl = 0;
mux->rt_mux_ctl = 0;
mux->rd_mux_ctl = 0;
mux->reg_input_mux_ctl = 0;
mux->alu_mux_ctl = 0;
mux->arith_mux_ctl = 0;
alu_ctrl* alu = &control->alu_ctrls;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 1;
reg->nzp_we = 1;
reg->data_we = 0;
int sub_op = INSN_5_3(I);
switch(sub_op_imm) {
// add immediate
case 1:
alu->arith_ctl = 0;
mux->arith_mux_ctl = 1;
break;
// not immediate
case 0:
switch(sub_op) {
// addition
case 0:
alu->arith_ctl = 0;
break;
// multiplication
case 1:
alu->arith_ctl = 1;
break;
// subtraction
case 2:
alu->arith_ctl = 2;
break;
// division
case 3:
alu->arith_ctl = 3;
break;
default:
break;
}
default:
break;
}
}
void decode_cmp(ctrl* control, unsigned short int I) {
int sub_op = INSN_8_7(I);
mux_ctl* mux = &control->mux_ctrls;
mux->pc_mux_ctl = 1;
mux->rs_mux_ctl = 2;
mux->rt_mux_ctl = 0;
mux->reg_input_mux_ctl = 0;
mux->alu_mux_ctl = 4;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 0;
reg->nzp_we = 1;
reg->data_we = 0;
switch(sub_op) {
alu_ctrl* alu = &control->alu_ctrls;
case 0:
alu->cmp_ctl = 0;
break;
case 1:
alu->cmp_ctl = 1;
break;
case 2:
alu->cmp_ctl = 2;
break;
case 3:
alu->cmp_ctl = 3;
break;
default:
break;
}
}
void decode_jsr(ctrl* control, unsigned short int I) {
mux_ctl* mux = &control->mux_ctrls;
mux->rd_mux_ctl = 1;
mux->reg_input_mux_ctl = 2;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 1;
reg->nzp_we = 1;
reg->data_we = 0;
unsigned int sub_op = INSN_11(I);
if (sub_op == 0) {
mux->pc_mux_ctl = 5;
}
else {
mux->pc_mux_ctl = 3;
}
}
void decode_logic(ctrl* control, unsigned short int I) {
int sub_op_imm = INSN_5(I);
int sub_op = INSN_5_3(I);
mux_ctl* mux = &control->mux_ctrls;
mux->pc_mux_ctl = 1;
mux->rs_mux_ctl = 0;
mux->rt_mux_ctl = 0;
mux->rd_mux_ctl = 0;
mux->reg_input_mux_ctl = 0;
mux->logic_mux_ctl = 0;
mux->alu_mux_ctl = 1;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 1;
reg->nzp_we = 1;
reg->data_we = 0;
alu_ctrl* alu = &control->alu_ctrls;
switch(sub_op_imm) {
// immediate
case 1:
alu->logic_ctl = 0;
mux->logic_mux_ctl = 1;
// non-immediate
case 0:
switch(sub_op) {
// and
case 0:
alu->logic_ctl = 0;
case 1:
// not
alu->logic_ctl = 1;
case 2:
// or
alu->logic_ctl = 2;
case 3:
// xor
alu->logic_ctl = 3;
default:
break;
}
default:
break;
}
}
void decode_ldr(ctrl* control, unsigned short int I) {
mux_ctl* mux = &control->mux_ctrls;
mux->pc_mux_ctl = 1;
mux->rs_mux_ctl = 0;
mux->rd_mux_ctl = 0;
mux->reg_input_mux_ctl = 1;
mux->arith_mux_ctl = 2;
mux->alu_mux_ctl = 0;
alu_ctrl* alu = &control->alu_ctrls;
alu->arith_ctl = 0;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 1;
reg->nzp_we = 1;
reg->data_we = 0;
}
void decode_str(ctrl* control, unsigned short int I) {
mux_ctl* mux = &control->mux_ctrls;
mux->pc_mux_ctl = 1;
mux->rs_mux_ctl = 0;
mux->rt_mux_ctl = 1;
mux->arith_mux_ctl = 2;
mux->alu_mux_ctl = 0;
alu_ctrl* alu = &control->alu_ctrls;
alu->arith_ctl = 0;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 0;
reg->nzp_we = 0;
reg->data_we = 1;
}
void decode_rti(ctrl* control, unsigned short int I) {
mux_ctl* mux = &control->mux_ctrls;
mux->pc_mux_ctl = 3;
mux->rs_mux_ctl = 1;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 0;
reg->nzp_we = 0;
reg->data_we = 0;
}
void decode_const(ctrl* control, unsigned short int I) {
mux_ctl* mux = &control->mux_ctrls;
mux->pc_mux_ctl = 1;
mux->rd_mux_ctl = 0;
mux->reg_input_mux_ctl = 0;
mux->alu_mux_ctl = 3;
alu_ctrl* alu = &control->alu_ctrls;
alu->const_ctl = 0;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 1;
reg->nzp_we = 1;
reg->data_we = 0;
}
void decode_shift(ctrl* control, unsigned short int I) {
mux_ctl* mux = &control->mux_ctrls;
mux->pc_mux_ctl = 1;
mux->rs_mux_ctl = 0;
mux->rd_mux_ctl = 0;
mux->reg_input_mux_ctl = 0;
mux->alu_mux_ctl = 2;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 1;
reg->nzp_we = 1;
reg->data_we = 0;
alu_ctrl* alu = &control->alu_ctrls;
int sub_op = INSN_5_4(I);
switch(sub_op) {
case 0:
alu->shift_ctl = 0;
break;
case 1:
alu->shift_ctl = 1;
break;
case 2:
alu->shift_ctl = 2;
break;
case 3:
mux->alu_mux_ctl = 0;
mux->rt_mux_ctl = 0;
mux->arith_mux_ctl = 0;
alu->arith_ctl = 4;
default:
break;
}
}
void decode_jmp(ctrl* control, unsigned short int I) {
int sub_op = INSN_11(I);
mux_ctl* mux = &control->mux_ctrls;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 0;
reg->nzp_we = 0;
reg->data_we = 0;
if (sub_op == 0) {
mux->pc_mux_ctl = 3;
mux->rs_mux_ctl = 0;
}
else {
mux->pc_mux_ctl = 2;
}
}
void decode_hiconst(ctrl* control, unsigned short int I) {
mux_ctl* mux = &control->mux_ctrls;
mux->pc_mux_ctl = 1;
mux->rs_mux_ctl = 2;
mux->rd_mux_ctl = 0;
mux->reg_input_mux_ctl = 0;
mux->alu_mux_ctl = 3;
alu_ctrl* alu = &control->alu_ctrls;
alu->const_ctl = 1;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 1;
reg->nzp_we = 1;
reg->data_we = 0;
}
void decode_trap(ctrl* control, unsigned short int I) {
mux_ctl* mux = &control->mux_ctrls;
mux->pc_mux_ctl = 4;
mux->rd_mux_ctl = 1;
mux->reg_input_mux_ctl = 2;
mux->rd_mux_ctl = 1;
reg_ctrl* reg = &control->reg_ctrls;
reg->reg_file_we = 1;
reg->nzp_we = 1;
reg->data_we = 0;
}
/*
* Decodes instruction and sets control signals accordingly.
* Params: pointer to control signals struct, instruction to decode
* HINT: Split up the individual decoding into multiple functions
* Here is a great place to use switch statements to call a specific decoding function
*/
void decode_instruction(ctrl* control, unsigned short int I) {
unsigned int op = INSN_OP(I);
switch(op) {
// BR
case 0:
decode_br(control,I);
break;
// arithmetic
case 1:
decode_arith(control, I);
break;
// CMP
case 2:
decode_cmp(control, I);
break;
// JSR
case 4:
decode_jsr(control, I);
break;
// logic
case 5:
decode_logic(control,I);
break;
// load
case 6:
decode_ldr(control,I);
break;
// store
case 7:
decode_str(control,I);
break;
// rti
case 8:
decode_rti(control,I);
break;
// const
case 9:
decode_const(control,I);
break;
// shift
case 10:
decode_shift(control,I);
break;
// jmpr/jmp
case 12:
decode_jmp(control,I);
break;
// hiconst
case 13:
decode_hiconst(control,I);
break;
// trap
case 15:
decode_trap(control,I);
break;
// default
default:
break;
}
}
/*
* Updates the machine state, simulating a single clock cycle.
* Check for exceptions here.
* Set privileged bit.
* Set registers.
* Returns 0 if update successful, a nonzero error code if unsuccessful.
* Params: pointer to current machine state, pointer to control signals struct
*/
int update_lc4_state(lc4_state* state, ctrl* control) {
reg_ctrl* reg = &control->reg_ctrls;
total_memory* mem = &state->memory;
unsigned short int I = mem->memory_array[state->PC];
decode_instruction(control,mem->memory_array[state->PC]);
set_registers(state,control,mem->memory_array[state->PC]);
if (check_exceptions(state) != 0) {
return 1;
}
unsigned short int rs_out = rs_mux(state,control);
unsigned short int rt_out = rt_mux(state,control);
unsigned short int alu_out = alu_mux(state,control,rs_out,rt_out);
unsigned short int reg_out = reg_input_mux(state,control,alu_out);
short int signed_reg_out = reg_out;
// write to register
if (reg->reg_file_we == 1) {
mem->R[state->rd_addr] = signed_reg_out;
}
// write to data
if (reg->data_we == 1) {
mem->memory_array[alu_out] = rt_out;
}
// write nzp
if (reg->nzp_we == 1) {
// nzp = z
if (signed_reg_out == 0) {
state->PSR = state->PSR & 0xFFF8;
state->PSR = state->PSR | 2;
} // nzp = p
else if (signed_reg_out > 0) {
state->PSR = state->PSR & 0xFFF8;
state->PSR = state->PSR | 1;
} // nzp = n
else {
state->PSR = state->PSR & 0xFFF8;
state->PSR = state->PSR | 4;
}
}
// check PSR[15]
if (INSN_OP(I) == 15) {
// set privilege bit to 1
state->PSR = state->PSR | 32768;
}
else if (INSN_OP(I) == 8) {
// set privilege bit to 0
state->PSR = state->PSR & 32767;
}
// print
print_operation(state,file,control);
// update state
state->PC = pc_mux(state,control,rs_out);
return 0;
}
/*
* Check if any exceptions have happened in the current state.
* Returns int corresponding to error code that occured, or 0 if no errors
* Param: pointer to current machine state
*/
int check_exceptions(lc4_state* state) {
total_memory* mem = &state->memory;
unsigned short int I = mem->memory_array[state->PC];
unsigned short int op = INSN_OP(I);
// accessing data as code
if ((state->PC >= 0x2000 && state->PC < 0x8000) || (state->PC >= 0xA000)) {
return 1;
}
// trying to read code as data
if (op == 6 || op == 7) {
unsigned short int add = mem->R[state->rs_addr] +
(signed)sext(6, INSN_5_0(I));
if ((add < 0x2000) || (add >= 0x8000 && add < 0xA000)) {
return 2;
}
if (add >= 0xA000) {
// trying to store data in OS without permission
unsigned short int psr_15 = (state->PSR & 0x8000) >> 15;
if (psr_15 != 1) {
return 3;
}
}
}
// trying to access OS when not in OS mode
if (state->PC >= 0x8000) {
if (((state->PSR & 0x8000) >> 15) != 1) {
return 3;
}
}
return 0;
}
/*
* Sets file pointer.
* Param: pointer to the file
*/
void set_fp(FILE* fp) {
file = fp;
}
/*
* Sets the values for rs_addr, rt_addr, and rd_addr fields in machine_state based
* on current instruction.
* Param: pointer to machine_state, current instruction
*/
void set_registers(lc4_state* state, ctrl* control, unsigned short int I) {
int op = INSN_OP(I);
// BR
if (op == 0) {
int sub_op = INSN_11_9(I);
if (sub_op != 0) {
state->imm = sext(9, INSN_8_0(I));
}
}
// arithmetic
else if (op == 1) {
int sub_op = INSN_5(I);
if (sub_op != 1) {
state->rt_addr = INSN_2_0(I);
}
else {
state->imm = sext(5, INSN_4_0(I));
}
state->rs_addr = INSN_8_6(I);
state->rd_addr = INSN_11_9(I);
}
// comparison
else if (op == 2) {
state->rs_addr = INSN_11_9(I);
int sub_op = INSN_8_7(I);
if (sub_op < 2) {
state->rt_addr = INSN_2_0(I);
}
else if (sub_op == 2) {
state->imm = sext(7, INSN_6_0(I));
}
else if (sub_op == 3) {
state->uimm = INSN_6_0(I);
}
}
// JSR
else if (op == 4) {
int sub_op = INSN_11(I);
if (sub_op == 0) {
state->rs_addr = INSN_8_7(I);
}
else {
state->imm = sext(11, INSN_10_0(I));
}
state->rd_addr = 7;
}
// logic
else if (op == 5) {
int sub_op = INSN_5(I);
if (sub_op != 1) {
sub_op = INSN_5_3(I);
if (sub_op != 1) {
state->rt_addr = INSN_2_0(I);
}
}
else {
state->imm = sext(5, INSN_4_0(I));
}
state->rd_addr = INSN_11_9(I);
state->rs_addr = INSN_8_6(I);
}
// ldr
else if (op == 6) {
state->rd_addr = INSN_11_9(I);
state->rs_addr = INSN_8_6(I);
state->imm = sext(6, INSN_5_0(I));
}
// str
else if (op == 7) {
state->rt_addr = INSN_11_9(I);
state->rs_addr = INSN_8_6(I);
state->imm = sext(6, INSN_5_0(I));
}
// rti
else if (op == 8) {
state->rs_addr = 7;
}
// const
else if (op == 9) {
state->rd_addr = INSN_11_9(I);
state->imm = sext(9, INSN_8_0(I));
}
// shift
else if (op == 10) {
state->rd_addr = INSN_11_9(I);
state->rs_addr = INSN_8_6(I);
int sub_op = INSN_5_4(I);
if (sub_op == 3) {
state->rt_addr = INSN_2_0(I);
}
else {
state->uimm = INSN_3_0(I);
}
}
// JMP
else if (op == 12) {
int sub_op = INSN_11(I);
if (sub_op == 0) {
state->rs_addr = INSN_8_6(I);
}
else {
state->imm = sext(11, INSN_10_0(I));
}
}
// hiconst
else if (op == 13) {
state->rd_addr = INSN_11_9(I);
state->uimm = INSN_7_0(I);
}
// trap
else if (op == 15) {
state->rd_addr = 7;
state->uimm = INSN_7_0(I);
}
}
/*
* Returns the current output of the RS mux.
* Param: pointer to current machine state, pointer to control signals struct
*/
unsigned short int rs_mux(lc4_state* state, ctrl* control) {
total_memory* mem = &state->memory;
return mem->R[state->rs_addr];
}
/*
* Returns the current output of the RT mux.
* Param: pointer to current machine state, pointer to control signals struct
*/
unsigned short int rt_mux(lc4_state* state, ctrl* control) {
total_memory* mem = &state->memory;
return mem->R[state->rt_addr];
}
/*
* Returns the current output of the ALU mux.
* Params: pointer to current machine state, RS mux output, RT mux output, pointer to control signals struct
*/
unsigned short int alu_mux(lc4_state* state, ctrl* control, unsigned short int rs_out, unsigned short int rt_out) {
mux_ctl* mux = &control->mux_ctrls;
alu_ctrl* alu = &control->alu_ctrls;
total_memory* mem = &state->memory;
unsigned short int I = mem->memory_array[state->PC];
// arithmetic ops
if (mux->alu_mux_ctl == 0) {
// register arithmetic
if (mux->arith_mux_ctl == 0) {
// addition
if (alu->arith_ctl == 0) {
return rs_out + rt_out;
}
// multiplication
else if (alu->arith_ctl == 1) {
return rs_out * rt_out;
}
// subtraction
else if (alu->arith_ctl == 2) {
return rs_out - rt_out;
}
// division
else if (alu->arith_ctl == 3) {
if (rt_out == 0) {
return 0;
}
else {
return rs_out/rt_out;
}
}
// mod
else if (alu->arith_ctl == 4) {
return rs_out%rt_out;
}
}
// immediate
else if (mux->arith_mux_ctl == 1 || mux->arith_mux_ctl == 2) {
if (alu->arith_ctl == 0) {
return rs_out + sext(5,INSN_4_0(I));
}
}
}
// logical ops
else if (mux->alu_mux_ctl == 1) {
// register logical
if (mux->logic_mux_ctl == 0) {
// and
if (alu->logic_ctl == 0) {
return rs_out & rt_out;
}
// not
else if (alu->logic_ctl == 1) {
return ~rs_out;
}
// or
else if (alu->logic_ctl == 2) {
return rs_out | rt_out;
}
// xor
else if (alu->logic_ctl == 3) {
return rs_out ^ rt_out;
}
}
// immediate logical
else if (mux->logic_mux_ctl == 1) {
// and
if (alu->logic_ctl == 0) {
return rs_out & state->imm;
}
}
}
// shifter
else if (mux->alu_mux_ctl == 2) {
// shift left logical
if (alu->shift_ctl == 0) {
return rs_out << state->uimm;
}
// shift right arithmetic
else if (alu->shift_ctl == 1) {
return (signed)rs_out >> state->uimm;
}
// shift right logical
else if (alu->shift_ctl == 2) {
return (unsigned)rs_out >> state->uimm;
}
}
// constants
else if (mux->alu_mux_ctl == 3) {
if (alu->const_ctl == 0) {
return state->imm;
}
else if (alu->const_ctl == 1) {
return ((rs_out & 0xFF) | (state->uimm << 8));
}
}
// comparator
else if (mux->alu_mux_ctl == 4) {
// signed register comparison
if (alu->cmp_ctl == 0) {
if (rs_out - rt_out == 0) {
return 0;
}
else if (rs_out - rt_out > 0) {
return 1;
}
else {
return -1;
}
}
// unsigned register comparison
else if (alu->cmp_ctl == 1) {
if (rs_out - (unsigned)rt_out == 0) {
return 0;
}
else if (rs_out - (unsigned)rt_out > 0) {
return 1;
}
else {
return -1;
}
}
// signed immediate comparison
else if (alu->cmp_ctl == 2) {
if (rs_out - state->imm == 0) {
return 0;
}
else if (rs_out - state->imm > 0) {
return 1;
}
else {
return -1;
}
}
// unsigned immediate comparison
else if (alu->cmp_ctl == 3) {
if (rs_out - state->uimm == 0) {
return 0;
}
else if (rs_out - state->uimm > 0) {
return 1;
}
else {
return -1;
}
}
}
return 0;
}
/*
* Returns the current output of the register input mux.
* Params: pointer to current machine state, current ALU mux output, pointer to control signals struct
*/
unsigned short int reg_input_mux(lc4_state* state, ctrl* control, unsigned short int alu_out) {
mux_ctl* mux = &control->mux_ctrls;
// alu
if (mux->reg_input_mux_ctl == 0) {
return alu_out;
}
// data
else if (mux->reg_input_mux_ctl == 1) {
total_memory* mem = &state->memory;
unsigned short int* mem_array = mem->memory_array;
return mem_array[alu_out];
}
// pc
else if (mux->reg_input_mux_ctl == 2) {
return state->PC + 1;
}
return 0;
}
/*
* Returns the current output of the PC mux.
* Params: current RS mux output, pointer to current machine state, pointer to control signals struct
*/
unsigned short int pc_mux(lc4_state* state, ctrl* control, unsigned short int rs_out) {
mux_ctl* mux = &control->mux_ctrls;
total_memory* mem = &state->memory;
unsigned short int I = mem->memory_array[state->PC];
// value of nzp register compared to bits I[11:9]
if (mux->pc_mux_ctl == 0) {
unsigned short int nzp = state->PSR & 7;
if ((nzp & INSN_11_9(I)) > 0) {
return state->PC + 1 + sext(9, INSN_8_0(I));
}
else {
return state->PC + 1;
}
}
// pc + 1
else if (mux->pc_mux_ctl == 1) {
return state->PC + 1;
}
// pc + 1 + sext(IMM11)
else if (mux->pc_mux_ctl == 2) {
return (state->PC + 1 + sext(12, INSN_11_0(I)));
}
// rs
else if (mux->pc_mux_ctl == 3) {
return mem->R[state->rs_addr];
}
// (0x8000 | UIMM8)
else if (mux->pc_mux_ctl == 4) {
return (0x8000 | INSN_8_0(I));
}
// pc & 0x8000 | IMM11 << 4
else if (mux->pc_mux_ctl == 5) {
return ((state->PC & 0x8000) | (INSN_11_0(I) << 4));
}
return 0;
}
/*
* Sign extends the given input unsigned short int to the inputted length
* Returns the sign extended value.
* Params: length to sign extend to, value to sign extend
*/
short int sext(unsigned int length, unsigned short int input) {
int sign = (input >> (length - 1)) & 0x1;
int mask = 0xFFFF;
if (sign == 0) {
return input;
}
else {
mask = mask << length;
return input | mask;
}
return 0;
}
/*
* Prints information about the current state
* Very helpful in debugging!
* Implementing this will also help with receiving partial credit.
*/