summaryrefslogtreecommitdiff
path: root/instruction.c
blob: 8b100931f10c9d041621dbdc0a73f75a86c8cebf (plain)
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
/*
 * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
 *
 * This file is part of nios2sim-ng.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License. See the file "COPYING" in the main directory of this archive
 * for more details.
 */

#include <stdio.h>

#include "nios2sim-ng.h"
#include "instruction.h"
#include "nios2.h"

#if 1
# define instr_dbg(fmt, args...) dbg(fmt, ##args)
#else
# define instr_dbg(fmt, args...)
#endif

static inline uint32_t get_opcode(uint32_t code)
{
	I_TYPE(instr, code);
	return instr->op;
}

static inline uint32_t get_opxcode(uint32_t code)
{
	R_TYPE(instr, code);
	return instr->opx6;
}

/*
 * Used as a placeholder for instructions not yet implemented by the simulator.
 */
static int unimplemented(struct nios2 *cpu, uint32_t code)
{
	info("Unsupported instruction %s (%08x) @ PC %08x\n",
			instruction_get_string(code), code, cpu->pc);

	return INSTR_UNIMPL;
}

/*
 * Used as a placeholder for all instructions which do not have an effect on the
 * simulator (e.g. flush, sync)
 */
static int nop(struct nios2 *cpu __unused, uint32_t code __unused)
{
	/* Nothing to do here */
	return PC_INC_NORMAL;
}

/*
 * J-Type instructions
 */

/*
 * ra <- PC + 4
 * PC <- (PC(31..28) : IMM26 * 4)
 */
static int call(struct nios2 *cpu, uint32_t code)
{
	J_TYPE(instr, code);

	cpu->gp_regs[ra] = cpu->pc + 4;
	cpu->pc = (cpu->pc & 0xF0000000) | (instr->imm26 * 4);

	if (cpu->pc == 0) {
		err("PC = 0\n");
		return INSTR_ERR;
	}

	return PC_INC_BY_INSTR;
}

/* PC <- (PC(31..28) : IMM26 * 4) */
static int jmpi(struct nios2 *cpu, uint32_t code)
{
	J_TYPE(instr, code);

	cpu->pc = (cpu->pc & 0xF0000000) | (instr->imm26 * 4);

	return PC_INC_BY_INSTR;
}

/*
 * I-Type instructions
 */

/* rB <- 0x000000 : Mem8[rA + @(IMM16)] */
static int ldbu(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;
	uint32_t addr = gp_regs[instr->a] + (int16_t) instr->imm16;
	uint8_t byte;

	if (nios2_load(cpu, addr, &byte, 1))
		return INSTR_ERR;

	gp_regs[instr->b] = (uint32_t) byte;

	return PC_INC_NORMAL;

}

/* rB <- rA + IMM16 */
static int addi(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->b] = gp_regs[instr->a] + (int16_t) (instr->imm16);

	return PC_INC_NORMAL;
}

/* Mem8[rA + @(IMM16)] <- rB(7..0) */
static int stb(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;
	uint32_t addr = gp_regs[instr->a] + (int16_t) instr->imm16;
	uint8_t data = gp_regs[instr->b] & 0xFF;

	if (nios2_store(cpu, addr, &data, 1))
		return INSTR_ERR;

	return PC_INC_NORMAL;
}

/* PC <- PC + 4 + IMM16 */
static int br(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);

	cpu->pc += 4 + (int16_t)(instr->imm16 & 0xFFFC);

	return PC_INC_NORMAL;
}

/* rB <- @(Mem8[rA + @(IMM16)]) */
static int ldb(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;
	uint32_t addr = gp_regs[instr->a] + (int16_t) instr->imm16;
	uint8_t byte;

	if (nios2_load(cpu, addr, &byte, 1))
		return INSTR_ERR;

	gp_regs[instr->b] = (int32_t) byte;

	return PC_INC_NORMAL;
}

/*
 * if ((signed) rA >= (signed) @(IMM16))
 *   rB <- 1
 * else
 *   rB <- 0
 */
static int cmpgei(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	if ((int32_t) gp_regs[instr->a] >= (int32_t) ((int16_t) instr->imm16))
		gp_regs[instr->b] = 1;
	else
		gp_regs[instr->b] = 0;

	return PC_INC_NORMAL;
}

/* rB <- 0x0000 : Mem16[rA + @IMM16)] */
static int ldhu(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;
	uint32_t addr = gp_regs[instr->a] + (int16_t) instr->imm16;
	uint16_t halfword;

	if (nios2_load(cpu, addr, &halfword, 2))
		return INSTR_ERR;

	gp_regs[instr->b] = (int32_t) halfword;

	return PC_INC_NORMAL;
}

/* rB <- rA & IMM16 */
static int andi(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->b] = gp_regs[instr->a] & instr->imm16;

	return PC_INC_NORMAL;
}

/* */
static int sth(struct nios2 *cpu __unused, uint32_t code __unused)
{
	/* TODO */
	return PC_INC_NORMAL;
}

/*
 * if ((signed) rA >= (signed) rB)
 *   PC <- PC + 4 + @(IMM16)
 * else
 *   PC <- PC + 4
 */
static int bge(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	if ((int32_t) gp_regs[instr->a] >= (int32_t) gp_regs[instr->b])
		cpu->pc += 4 + (instr->imm16 & 0xFFFC);
	else
		cpu->pc += 4;

	return PC_INC_BY_INSTR;
}

/* */
static int ldh(struct nios2 *cpu __unused, uint32_t code __unused)
{
	/* TODO */
	return PC_INC_NORMAL;
}

/*
 * if ((signed) rA < (signed) @(IMM16))
 *   rB <- 1
 * else
 *   rB <- 0
 */
static int cmplti(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	if ((int32_t) gp_regs[instr->a] < (int32_t) ((int16_t) instr->imm16))
		gp_regs[instr->b] = 1;
	else
		gp_regs[instr->b] = 0;

	return PC_INC_NORMAL;
}

/* Initializes the data cache line currently caching address rA + @(IMM16) */
static int initda(struct nios2 *cpu __unused, uint32_t code __unused)
{
	/* TODO */
	return PC_INC_NORMAL;
}

/* rB <- rA | IMM16 */
static int ori(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->b] = gp_regs[instr->a] | instr->imm16;
	return PC_INC_NORMAL;
}

/* */
static int stw(struct nios2 *cpu __unused, uint32_t code __unused)
{
	/* TODO */

	return PC_INC_NORMAL;
}

/*
 * if ((signed) rA < (signed) rB)
 *   PC <- PC + 4 + @(IMM16)
 * else
 *   PC <- PC + 4
 */
static int blt(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	if ((int32_t) gp_regs[instr->a] < (int32_t) gp_regs[instr->b])
		cpu->pc += 4 + (int16_t)(instr->imm16 & 0xFFFC);
	else
		cpu->pc += 4;

	return PC_INC_BY_INSTR;
}

/* rB <- @(Mem32[rA + @(IMM16)]) */
static int ldw(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;
	uint32_t addr = gp_regs[instr->a] + (int16_t) instr->imm16;

	if (nios2_load(cpu, addr, &gp_regs[instr->b], 4))
		return INSTR_ERR;

	return PC_INC_NORMAL;
}

/* rB <- rA ^ IMM16 */
static int xori(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->b] = gp_regs[instr->a] ^ instr->imm16;
	return PC_INC_NORMAL;
}

/*
 * if (rA != rB)
 *   PC <- PC + 4 + @(IMM16)
 * else
 *   PC <- PC + 4
 */
static int bne(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	if (gp_regs[instr->a] != gp_regs[instr->b])
		cpu->pc += 4 + (int16_t)(instr->imm16 & 0xFFFC);
	else
		cpu->pc += 4;

	return PC_INC_BY_INSTR;
}

/* rB <- (rA * @(IMM16))(31..0) */
static int muli(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;
	uint64_t tmp;

	/* Raise exception if instruction is not implemented */
	if (!cpu->has_mul)
		return EXCEPTION(cpu, NIOS2_EX_UNIMPLEMENTED);

	tmp = gp_regs[instr->a] * ((int16_t) instr->imm16);
	gp_regs[instr->b] = (uint32_t) (tmp & BIT_MASK_FILL(32));
	return PC_INC_NORMAL;
}

/*
 * if (rA == rB)
 *   PC <- PC + 4 + @(IMM16)
 * else
 *   PC <- PC + 4
 */
static int beq(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	if (gp_regs[instr->a] == gp_regs[instr->b])
		cpu->pc += 4 + (int16_t)(instr->imm16 & 0xFFFC);
	else
		cpu->pc += 4;

	return PC_INC_BY_INSTR;
}

/* rB <- rA & (IMM16 : 0x0000) */
static int andhi(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->b] = gp_regs[instr->a] & (instr->imm16 << 16);

	return PC_INC_NORMAL;
}

/*
 * if (rA >= rB)
 *   PC <- PC + 4 + @(IMM16)
 * else
 *   PC <- PC + 4
 */
static int bgeu(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	if (gp_regs[instr->a] >= gp_regs[instr->b])
		cpu->pc += 4 + (int16_t)(instr->imm16 & 0xFFFC);
	else
		cpu->pc += 4;

	return PC_INC_BY_INSTR;
}

/* */
static int initd(struct nios2 *cpu __unused, uint32_t code __unused)
{
	/* TODO */

	return PC_INC_NORMAL;
}

/* rB <- rA | (IMM16 : 0x0000) */
static int orhi(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->b] = gp_regs[instr->a] | (instr->imm16 << 16);

	return PC_INC_NORMAL;
}

/*
 * if ((unsigned) rA < (unsigned) rB)
 *   PC <- PC + 4 + @(IMM16)
 * else
 *   PC <- PC + 4
 */
static int bltu(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	if (gp_regs[instr->a] < gp_regs[instr->b])
		cpu->pc += 4 + (int16_t)(instr->imm16 & 0xFFFC);
	else
		cpu->pc += 4;

	return PC_INC_BY_INSTR;
}

/* Prototype only, defined below */
static int handle_r_type_instr(struct nios2 *cpu, uint32_t code);

/* rB <- rA ^ (IMM16 : 0x0000) */
static int xorhi(struct nios2 *cpu, uint32_t code)
{
	I_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->b] = gp_regs[instr->a] ^ (instr->imm16 << 16);

	return PC_INC_NORMAL;
}

static struct instruction i_type_instructions[I_TYPE_COUNT] = {
	[CALL]		= INSTRUCTION(call),
	[JMPI]		= INSTRUCTION(jmpi),
	[LDBU]		= INSTRUCTION(ldbu),
	[ADDI]		= INSTRUCTION(addi),
	[STB]		= INSTRUCTION(stb),
	[BR]		= INSTRUCTION(br),
	[LDB]		= INSTRUCTION(ldb),
	[CMPGEI]	= INSTRUCTION(cmpgei),
	[LDHU]		= INSTRUCTION(ldhu),
	[ANDI]		= INSTRUCTION(andi),
	[STH]		= INSTRUCTION(sth),
	[BGE]		= INSTRUCTION(bge),
	[LDH]		= INSTRUCTION(ldh),
	[CMPLTI]	= INSTRUCTION(cmplti),
	[INITDA]	= INSTRUCTION(initda),
	[ORI]		= INSTRUCTION(ori),
	[STW]		= INSTRUCTION(stw),
	[BLT]		= INSTRUCTION(blt),
	[LDW]		= INSTRUCTION(ldw),
	[CMPNEI]	= INSTRUCTION_UNIMPLEMENTED(cmpnei),
	[FLUSHDA]	= INSTRUCTION_NOP(flushda),
	[XORI]		= INSTRUCTION(xori),
	[BNE]		= INSTRUCTION(bne),
	[CMPEQI]	= INSTRUCTION_UNIMPLEMENTED(cmpeqi),
	[LDBUIO]	= INSTRUCTION_UNIMPLEMENTED(ldbuio),
	[MULI]		= INSTRUCTION(muli),
	[STBIO]		= INSTRUCTION_UNIMPLEMENTED(stbio),
	[BEQ]		= INSTRUCTION(beq),
	[LDBIO]		= INSTRUCTION_UNIMPLEMENTED(ldbio),
	[CMPGEUI]	= INSTRUCTION_UNIMPLEMENTED(cmpgeui),
	[LDHUIO]	= INSTRUCTION_UNIMPLEMENTED(ldhuio),
	[ANDHI]		= INSTRUCTION(andhi),
	[STHIO]		= INSTRUCTION_UNIMPLEMENTED(sthio),
	[BGEU]		= INSTRUCTION(bgeu),
	[LDHIO]		= INSTRUCTION_UNIMPLEMENTED(ldhio),
	[CMPLTUI]	= INSTRUCTION_UNIMPLEMENTED(cmpltui),
	[CUSTOM]	= INSTRUCTION_UNIMPLEMENTED(custom),
	[INITD]		= INSTRUCTION(initd),
	[ORHI]		= INSTRUCTION(orhi),
	[STWIO]		= INSTRUCTION_UNIMPLEMENTED(stwio),
	[BLTU]		= INSTRUCTION(bltu),
	[LDWIO]		= INSTRUCTION_UNIMPLEMENTED(ldwio),
	[R_TYPE]	= { "<R-type instruction>", handle_r_type_instr },
	[FLUSHD]	= INSTRUCTION_NOP(flushd),
	[XORHI]		= INSTRUCTION(xorhi),
};

/*
 * R-Type instructions
 */

/*
 * status <- estatus
 * PC <- ea
 */
static int eret(struct nios2 *cpu, uint32_t code __unused)
{
	cpu->ctrl_regs[status] = cpu->ctrl_regs[estatus];
	cpu->pc = cpu->gp_regs[ea];

	return PC_INC_BY_INSTR;
}

/* rC <- rA rotated left IMM5 bit positions */
static int roli(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;
	uint32_t a = gp_regs[instr->a];
	uint32_t shift_bits = instr->imm5;

	gp_regs[instr->c] = (a  << shift_bits) | (a >> (32 - shift_bits));

	return PC_INC_NORMAL;
}

/* rC <- rA rotated left rB(4..0) bit positions */
static int rol(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;
	uint32_t a = gp_regs[instr->a];
	uint32_t shift_bits = gp_regs[instr->b] & BIT_MASK_FILL(5);

	gp_regs[instr->c] = (a << shift_bits) | (a >> (32 - shift_bits));

	return PC_INC_NORMAL;
}

/* */
static int flushp(struct nios2 *cpu __unused, uint32_t code __unused)
{
	/* TODO */

	return PC_INC_NORMAL;
}

/* PC <- ra */
static int ret(struct nios2 *cpu, uint32_t code __unused)
{
	cpu->pc = cpu->gp_regs[ra];

	return PC_INC_BY_INSTR;
}

/* rC <- ~(A | rB) */
static int nor(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->c] = ~(gp_regs[instr->a] | gp_regs[instr->b]);

	return PC_INC_NORMAL;
}

/*  rC <- rA rotated right rb(4..0) bit positions */
static int ror(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;
	uint32_t a = gp_regs[instr->a];
	uint32_t shift_bits = gp_regs[instr->b] & BIT_MASK_FILL(5);

	gp_regs[instr->c] = (a << (32 - shift_bits)) | (a >> shift_bits);

	return PC_INC_NORMAL;
}

/* */
static int flushi(struct nios2 *cpu __unused, uint32_t code __unused)
{
	/* TODO */

	return PC_INC_NORMAL;
}

/* PC <- rA */
static int jmp(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);

	cpu->pc = cpu->gp_regs[instr->a];

	return PC_INC_BY_INSTR;
}

/* rC <- rA & rB */
static int and(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->c] = gp_regs[instr->a] & gp_regs[instr->b];

	return PC_INC_NORMAL;
}

/*
 * if ((signed) rA < (signed) rB)
 *   rC <- 1
 * else
 *   rC <- 0
 */
static int cmplt(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	if (((int32_t) gp_regs[instr->a]) < ((int32_t) gp_regs[instr->b]))
		gp_regs[instr->c] = 1;
	else
		gp_regs[instr->c] = 0;

	return PC_INC_NORMAL;
}

/* rC <- rA << IMM5 */
static int slli(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->c] = gp_regs[instr->a] << instr->imm5;

	return PC_INC_NORMAL;
}

/* rC <- rA << rB(4..0) */
static int sll(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->c] = gp_regs[instr->a] << (gp_regs[instr->b] & BIT_MASK_FILL(5));

	return PC_INC_NORMAL;
}

/* rC <- rA | rB */
static int or(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->c] = gp_regs[instr->a] | gp_regs[instr->b];

	return PC_INC_NORMAL;
}

/*
 * if (rA != rB)
 *   rC <- 1
 * else
 *   rC <- 0
 */
static int cmpne(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	if (gp_regs[instr->a] != gp_regs[instr->b])
		gp_regs[instr->c] = 1;
	else
		gp_regs[instr->c] = 0;

	return PC_INC_NORMAL;
}

/* rC <- (unsigned) rA >> ((unsigned) IMM5)*/
static int srli(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->c] = gp_regs[instr->a] >> instr->imm5;

	return PC_INC_NORMAL;
}

/* rC <- (unsigned) rA >> ((unsigned) rB(4..0))*/
static int srl(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->c] = gp_regs[instr->a] >> (gp_regs[instr->b] & BIT_MASK_FILL(5));

	return PC_INC_NORMAL;
}

/* rC <- PC + 4 */
static int nextpc(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);

	cpu->gp_regs[instr->c] = cpu->pc + 4;

	return PC_INC_NORMAL;
}

/*
 * ra <- PC + 4
 * PC <- rA
 */
static int callr(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);

	cpu->gp_regs[ra] = cpu->pc + 4;
	cpu->pc = cpu->gp_regs[instr->a];

	return PC_INC_BY_INSTR;
}

/* rC <- rA ^ rB */
static int xor(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->c] = gp_regs[instr->a] ^ gp_regs[instr->b];

	return PC_INC_NORMAL;
}

/*
 * if (rA == rB)
 *   rC <- 1
 * else
 *   rC <- 0
 */
static int cmpeq(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	if (gp_regs[instr->a] == gp_regs[instr->b])
		gp_regs[instr->c] = 1;
	else
		gp_regs[instr->c] = 0;

	return PC_INC_NORMAL;
}

/* rC <- rA / rB */
static int divu(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	if (!cpu->has_div)
		return EXCEPTION(cpu, NIOS2_EX_UNIMPLEMENTED);

	/* Division by zero? */
	if (gp_regs[instr->b] == 0)
		return EXCEPTION(cpu, NIOS2_EX_DIV_ERR);

	gp_regs[instr->c] = gp_regs[instr->a] / gp_regs[instr->b];

	return PC_INC_NORMAL;
}

/* rC <- rA / rB */
static int div(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;
	int32_t a, b;

	if (!cpu->has_div)
		return EXCEPTION(cpu, NIOS2_EX_UNIMPLEMENTED);

	a = (int32_t) gp_regs[instr->a];
	b = (int32_t) gp_regs[instr->b];

	/* Division by zero? */
	if (b == 0)
		return EXCEPTION(cpu, NIOS2_EX_DIV_ERR);
	/* Divide overflow? */
	if ((uint32_t) a == 0x80000000 && (uint32_t) b == 0xFFFFFFFF)
		return EXCEPTION(cpu, NIOS2_EX_DIV_ERR);

	gp_regs[instr->c] = (uint32_t)(a / b);

	return PC_INC_NORMAL;
}

/* rC <- ctlN */
static int rdctl(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);

	/* Instruction only allowed in supervisor mode */
	if (!nios2_in_supervisor_mode(cpu))
		return EXCEPTION(cpu, NIOS2_EX_SUPERVISOR_ONLY_I);

	cpu->gp_regs[instr->c] = cpu->ctrl_regs[instr->imm5];

	return PC_INC_NORMAL;
}

/* */
static int initi(struct nios2 *cpu __unused, uint32_t code __unused)
{
	/* TODO */

	return PC_INC_NORMAL;
}

/*
 * estatus <- status
 * PIE <- 0
 * U <- 0
 * ea <- PC + 4
 * PC <- exception handler address
 */
static int trap(struct nios2 *cpu __unused, uint32_t code __unused)
{
	return EXCEPTION(cpu, NIOS2_EX_TRAP);
}

/* ctlN <- rA */
static int wrctl(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);

	/* Instruction only allowed in supervisor mode */
	if (!nios2_in_supervisor_mode(cpu))
		return EXCEPTION(cpu, NIOS2_EX_SUPERVISOR_ONLY_I);

	cpu->ctrl_regs[instr->imm5] = cpu->gp_regs[instr->a];

	return PC_INC_NORMAL;
}

/* rC <- rA + rB */
static int add(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->c] = gp_regs[instr->a] + gp_regs[instr->b];

	return PC_INC_NORMAL;
}

/* */
static int __break(struct nios2 *cpu, uint32_t code)
{
	return INSTR_BREAK;
}

/* rC <- rA - rB */
static int sub(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->c] = gp_regs[instr->a] - gp_regs[instr->b];

	return PC_INC_NORMAL;
}

/* rC <- (signed) rA >> ((unsigned) IMM5) */
static int srai(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->c] = ((int32_t) gp_regs[instr->a]) >> instr->imm5;

	return PC_INC_NORMAL;
}

/* rC <- (signed) rA >> ((unsigned) rB(4..0)) */
static int sra(struct nios2 *cpu, uint32_t code)
{
	R_TYPE(instr, code);
	uint32_t *gp_regs = cpu->gp_regs;

	gp_regs[instr->c] = ((int32_t) gp_regs[instr->a]) >> (gp_regs[instr->b] & 0x1F);

	return PC_INC_NORMAL;
}

static struct instruction r_type_instructions[R_TYPE_COUNT] = {
	[ERET]		= INSTRUCTION(eret),
	[ROLI]		= INSTRUCTION(roli),
	[ROL]		= INSTRUCTION(rol),
	[FLUSHP]	= INSTRUCTION(flushp),
	[RET]		= INSTRUCTION(ret),
	[NOR]		= INSTRUCTION(nor),
	[MULXUU]	= INSTRUCTION_UNIMPLEMENTED(mulxuu),
	[CMPGE]		= INSTRUCTION_UNIMPLEMENTED(cmpge),
	[BRET]		= INSTRUCTION_UNIMPLEMENTED(bret),
	[ROR]		= INSTRUCTION(ror),
	[FLUSHI]	= INSTRUCTION(flushi),
	[JMP]		= INSTRUCTION(jmp),
	[AND]		= INSTRUCTION(and),
	[CMPLT]		= INSTRUCTION(cmplt),
	[SLLI]		= INSTRUCTION(slli),
	[SLL]		= INSTRUCTION(sll),
	[OR]		= INSTRUCTION(or),
	[MULXSU]	= INSTRUCTION_UNIMPLEMENTED(mulxsu),
	[CMPNE]		= INSTRUCTION(cmpne),
	[SRLI]		= INSTRUCTION(srli),
	[SRL]		= INSTRUCTION(srl),
	[NEXTPC]	= INSTRUCTION(nextpc),
	[CALLR]		= INSTRUCTION(callr),
	[XOR]		= INSTRUCTION(xor),
	[MULXSS]	= INSTRUCTION_UNIMPLEMENTED(mulxss),
	[CMPEQ]		= INSTRUCTION(cmpeq),
	[DIVU]		= INSTRUCTION(divu),
	[DIV]		= INSTRUCTION(div),
	[RDCTL]		= INSTRUCTION(rdctl),
	[MUL]		= INSTRUCTION_UNIMPLEMENTED(mul),
	[CMPGEU]	= INSTRUCTION_UNIMPLEMENTED(cmpgeu),
	[INITI]		= INSTRUCTION(initi),
	[TRAP]		= INSTRUCTION(trap),
	[WRCTL]		= INSTRUCTION(wrctl),
	[CMPLTU]	= INSTRUCTION_UNIMPLEMENTED(cmpltu),
	[ADD]		= INSTRUCTION(add),
	[BREAK]		= { "break", __break },
	[SYNC]		= INSTRUCTION(nop),
	[SUB]		= INSTRUCTION(sub),
	[SRAI]		= INSTRUCTION(srai),
	[SRA]		= INSTRUCTION(sra),
};

static int handle_r_type_instr(struct nios2 *cpu, uint32_t code)
{
	uint32_t opx;
	instruction_handler handle_instr;

	opx = get_opxcode(code);
	if (unlikely(opx >= R_TYPE_COUNT))
		return INSTR_ERR;

	instr_dbg("  R: %s (%08x)\n", r_type_instructions[opx].name, code);
	handle_instr = r_type_instructions[opx].handler;
	if (unlikely(handle_instr == NULL))
		return INSTR_ERR;

	return handle_instr(cpu, code);
}

instruction_handler instruction_get_handler(uint32_t code)
{
	uint32_t op = get_opcode(code);

	if (unlikely(op >= I_TYPE_COUNT))
		return NULL;
	instr_dbg("I: %s (%08x)\n", i_type_instructions[op].name, code);
	return i_type_instructions[op].handler;
}

const char *instruction_get_string(uint32_t code)
{
	uint32_t op = get_opcode(code);

	if (unlikely(op >= I_TYPE_COUNT))
		return "";
	else if (op == R_TYPE) {
		uint32_t opx = get_opxcode(code);
		if (unlikely(opx >= R_TYPE_COUNT))
			return "";
		return r_type_instructions[opx].name;
	} else
		return i_type_instructions[op].name;
}