-
Notifications
You must be signed in to change notification settings - Fork 19
/
mc6809i.v
4156 lines (3653 loc) · 124 KB
/
mc6809i.v
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
`timescale 1ns / 1ns
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer: Greg Miller
// Copyright (c) 2016, Greg Miller
//
// Create Date: 14:26:59 08/13/2016
// Design Name:
// Module Name: mc6809
// Project Name: Cycle-Accurate 6809 Core
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies: Intended to be standalone Vanilla Verilog.
//
// Revision:
// Revision 1.0 - Initial Release
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
//
// The 6809 has incomplete instruction decoding. A collection of instructions, if met, end up actually behaving like
// a binary-adjacent neighbor.
//
// The soft core permits three different behaviors for this situation, controlled by the instantiation parameter
// ILLEGAL_INSTRUCTIONS
//
// "GHOST" - Mimic the 6809's incomplete decoding. This is as similar to a hard 6809 as is practical. [DEFAULT]
//
// "STOP" - Cause the soft core to cease execution, placing $DEAD on the address bus and R/W to 'read'. Interrupts,
// bus control (/HALT, /DMABREQ), etc. are ignored. The core intentionally seizes in this instance.
// (Frankly, this is useful when making changes to the core and you have a logic analyzer connected.)
//
// "IGNORE"- Cause the soft core to merely ignore illegal instructions. It will consider them 1-byte instructions and
// attempt to fetch and run an exception 1 byte later.
//
module mc6809i
#(
parameter ILLEGAL_INSTRUCTIONS="GHOST"
)
(
input [7:0] D,
output [7:0] DOut,
output [15:0] ADDR,
output RnW,
input E,
input Q,
output BS,
output BA,
input nIRQ,
input nFIRQ,
input nNMI,
output AVMA,
output BUSY,
output LIC,
input nHALT,
input nRESET,
input nDMABREQ,
output [111:0] RegData
);
reg [7:0] DOutput;
assign DOut = DOutput;
reg RnWOut; // Combinatorial
reg rLIC;
assign LIC = rLIC;
reg rAVMA;
assign AVMA = rAVMA;
reg rBUSY;
assign BUSY = rBUSY;
// Bus control
// BS BA
// 0 0 normal (CPU running, CPU is master)
// 0 1 Interrupt Ack
// 1 0 Sync Ack
// 1 1 CPU has gone high-Z on A, D, R/W
//
assign RnW = RnWOut;
/////////////////////////////////////////////////
// Vectors
`define RESET_VECTOR 16'HFFFE
`define NMI_VECTOR 16'HFFFC
`define SWI_VECTOR 16'HFFFA
`define IRQ_VECTOR 16'HFFF8
`define FIRQ_VECTOR 16'HFFF6
`define SWI2_VECTOR 16'HFFF4
`define SWI3_VECTOR 16'HFFF2
`define Reserved_VECTOR 16'HFFF0
//////////////////////////////////////////////////////
// Latched registers
//
// The last-latched copy.
reg [7:0] a;
reg [7:0] b;
reg [15:0] x;
reg [15:0] y;
reg [15:0] u;
reg [15:0] s;
reg [15:0] pc;
reg [7:0] dp;
reg [7:0] cc;
reg [15:0] tmp;
reg [15:0] addr;
reg [15:0] ea;
// Debug ability to export register contents
assign RegData[7:0] = a;
assign RegData[15:8] = b;
assign RegData[31:16] = x;
assign RegData[47:32] = y;
assign RegData[63:48] = s;
assign RegData[79:64] = u;
assign RegData[87:80] = cc;
assign RegData[95:88] = dp;
assign RegData[111:96] = pc;
// The values as being calculated
reg [7:0] a_nxt;
reg [7:0] b_nxt;
reg [15:0] x_nxt;
reg [15:0] y_nxt;
reg [15:0] u_nxt;
reg [15:0] s_nxt;
reg [15:0] pc_nxt;
reg [7:0] dp_nxt;
reg [7:0] cc_nxt;
reg [15:0] addr_nxt;
reg [15:0] ea_nxt;
reg [15:0] tmp_nxt;
reg BS_nxt;
reg BA_nxt;
// for ADDR, BS/BA, assign them to the flops
assign BS = BS_nxt;
assign BA = BA_nxt;
assign ADDR=addr_nxt;
localparam CC_E= 8'H80;
localparam CC_F= 8'H40;
localparam CC_H= 8'H20;
localparam CC_I= 8'H10;
localparam CC_N= 8'H08;
localparam CC_Z= 8'H04;
localparam CC_V= 8'H02;
localparam CC_C= 8'H01;
localparam CC_E_BIT= 3'd7;
localparam CC_F_BIT= 3'd6;
localparam CC_H_BIT= 3'd5;
localparam CC_I_BIT= 3'd4;
localparam CC_N_BIT= 3'd3;
localparam CC_Z_BIT= 3'd2;
localparam CC_V_BIT= 3'd1;
localparam CC_C_BIT= 3'd0;
// Convenience calculations
reg [15:0] pc_p1;
reg [15:0] pc_p2;
reg [15:0] pc_p3;
reg [15:0] s_p1;
reg [15:0] s_m1;
reg [15:0] u_p1;
reg [15:0] u_m1;
reg [15:0] addr_p1;
reg [15:0] ea_p1;
//////////////////////////////////////////////////////
// NMI Mask
//
// NMI is supposed to be masked - despite the name - until the 6809 loads a value into S.
// Frankly, I'm cheating slightly. If someone does a LDS #$0, it won't disable the mask. Pretty much anything else
// that changes the value of S from the default (which is currently $0) will clear the mask. A reset will set the mask again.
reg NMIMask;
reg NMILatched;
reg NMISample;
reg NMISample2;
reg NMIClear;
reg NMIClear_nxt;
wire wNMIClear = NMIClear;
reg IRQLatched;
reg IRQSample;
reg IRQSample2;
reg FIRQLatched;
reg FIRQSample;
reg FIRQSample2;
reg HALTLatched;
reg HALTSample;
reg HALTSample2;
reg DMABREQLatched;
reg DMABREQSample;
reg DMABREQSample2;
// Interrupt types
localparam INTTYPE_NMI = 3'H0 ;
localparam INTTYPE_IRQ = 3'H1 ;
localparam INTTYPE_FIRQ = 3'H2 ;
localparam INTTYPE_SWI = 3'H3 ;
localparam INTTYPE_SWI2 = 3'H4 ;
localparam INTTYPE_SWI3 = 3'H5 ;
reg [2:0] IntType;
reg [2:0] IntType_nxt;
//////////////////////////////////////////////////////
// Instruction Fetch Details
//
reg InstPage2;
reg InstPage3;
reg InstPage2_nxt;
reg InstPage3_nxt;
reg [7:0] Inst1;
reg [7:0] Inst2;
reg [7:0] Inst3;
reg [7:0] Inst1_nxt;
reg [7:0] Inst2_nxt;
reg [7:0] Inst3_nxt;
localparam CPUSTATE_RESET = 7'd0;
localparam CPUSTATE_RESET0 = 7'd1;
localparam CPUSTATE_RESET2 = 7'd3;
localparam CPUSTATE_FETCH_I1 = 7'd4;
localparam CPUSTATE_FETCH_I1V2 = 7'd5;
localparam CPUSTATE_FETCH_I2 = 7'd8;
localparam CPUSTATE_LBRA_OFFSETLOW = 7'd17;
localparam CPUSTATE_LBRA_DONTCARE = 7'd18;
localparam CPUSTATE_LBRA_DONTCARE2 = 7'd19;
localparam CPUSTATE_BRA_DONTCARE = 7'd20;
localparam CPUSTATE_BSR_DONTCARE1 = 7'd21;
localparam CPUSTATE_BSR_DONTCARE2 = 7'd22;
localparam CPUSTATE_BSR_RETURNLOW = 7'd23;
localparam CPUSTATE_BSR_RETURNHIGH = 7'd24;
localparam CPUSTATE_TFR_DONTCARE1 = 7'd26;
localparam CPUSTATE_TFR_DONTCARE2 = 7'd27;
localparam CPUSTATE_TFR_DONTCARE3 = 7'd28;
localparam CPUSTATE_TFR_DONTCARE4 = 7'd29;
localparam CPUSTATE_EXG_DONTCARE1 = 7'd30;
localparam CPUSTATE_EXG_DONTCARE2 = 7'd31;
localparam CPUSTATE_EXG_DONTCARE3 = 7'd32;
localparam CPUSTATE_EXG_DONTCARE4 = 7'd33;
localparam CPUSTATE_EXG_DONTCARE5 = 7'd34;
localparam CPUSTATE_EXG_DONTCARE6 = 7'd35;
localparam CPUSTATE_ABX_DONTCARE = 7'd36;
localparam CPUSTATE_RTS_HI = 7'd38;
localparam CPUSTATE_RTS_LO = 7'd39;
localparam CPUSTATE_RTS_DONTCARE2 = 7'd40;
localparam CPUSTATE_16IMM_LO = 7'd41;
localparam CPUSTATE_ALU16_DONTCARE = 7'd42;
localparam CPUSTATE_DIRECT_DONTCARE = 7'd43;
localparam CPUSTATE_ALU_EA = 7'd44;
localparam CPUSTATE_ALU_DONTCARE = 7'd46;
localparam CPUSTATE_ALU_WRITEBACK = 7'd47;
localparam CPUSTATE_LD16_LO = 7'd48;
localparam CPUSTATE_ST16_LO = 7'd49;
localparam CPUSTATE_ALU16_LO = 7'd50;
localparam CPUSTATE_JSR_DONTCARE = 7'd53;
localparam CPUSTATE_JSR_RETLO = 7'd54;
localparam CPUSTATE_JSR_RETHI = 7'd55;
localparam CPUSTATE_EXTENDED_ADDRLO = 7'd56;
localparam CPUSTATE_EXTENDED_DONTCARE = 7'd57;
localparam CPUSTATE_INDEXED_BASE = 7'd58;
localparam CPUSTATE_IDX_DONTCARE3 = 7'd60;
localparam CPUSTATE_IDX_OFFSET_LO = 7'd61;
localparam CPUSTATE_IDX_16OFFSET_LO = 7'd62;
localparam CPUSTATE_IDX_16OFF_DONTCARE0= 7'd63;
localparam CPUSTATE_IDX_16OFF_DONTCARE1= 7'd64;
localparam CPUSTATE_IDX_16OFF_DONTCARE2= 7'd65;
localparam CPUSTATE_IDX_16OFF_DONTCARE3= 7'd66;
localparam CPUSTATE_IDX_DOFF_DONTCARE1 = 7'd68;
localparam CPUSTATE_IDX_DOFF_DONTCARE2 = 7'd69;
localparam CPUSTATE_IDX_DOFF_DONTCARE3 = 7'd70;
localparam CPUSTATE_IDX_PC16OFF_DONTCARE = 7'd71;
localparam CPUSTATE_IDX_EXTIND_LO = 7'd72;
localparam CPUSTATE_IDX_EXTIND_DONTCARE = 7'd73;
localparam CPUSTATE_INDIRECT_HI = 7'd74;
localparam CPUSTATE_INDIRECT_LO = 7'd75;
localparam CPUSTATE_INDIRECT_DONTCARE = 7'd76;
localparam CPUSTATE_MUL_ACTION = 7'd77;
localparam CPUSTATE_PSH_DONTCARE1 = 7'd80;
localparam CPUSTATE_PSH_DONTCARE2 = 7'd81;
localparam CPUSTATE_PSH_DONTCARE3 = 7'd82;
localparam CPUSTATE_PSH_ACTION = 7'd83;
localparam CPUSTATE_PUL_DONTCARE1 = 7'd84;
localparam CPUSTATE_PUL_DONTCARE2 = 7'd85;
localparam CPUSTATE_PUL_ACTION = 7'd86;
localparam CPUSTATE_NMI_START = 7'd87;
localparam CPUSTATE_IRQ_DONTCARE = 7'd88;
localparam CPUSTATE_IRQ_START = 7'd89;
localparam CPUSTATE_IRQ_DONTCARE2 = 7'd90;
localparam CPUSTATE_IRQ_VECTOR_HI = 7'd91;
localparam CPUSTATE_IRQ_VECTOR_LO = 7'd92;
localparam CPUSTATE_FIRQ_START = 7'd93;
localparam CPUSTATE_CC_DONTCARE = 7'd94;
localparam CPUSTATE_SWI_START = 7'd95;
localparam CPUSTATE_TST_DONTCARE1 = 7'd96;
localparam CPUSTATE_TST_DONTCARE2 = 7'd97;
localparam CPUSTATE_DEBUG = 7'd98;
localparam CPUSTATE_16IMM_DONTCARE = 7'd99;
localparam CPUSTATE_HALTED = 7'd100;
localparam CPUSTATE_HALT_EXIT2 = 7'd102;
localparam CPUSTATE_STOP = 7'd105;
localparam CPUSTATE_STOP2 = 7'd106;
localparam CPUSTATE_STOP3 = 7'd107;
localparam CPUSTATE_CWAI = 7'd108;
localparam CPUSTATE_CWAI_DONTCARE1 = 7'd109;
localparam CPUSTATE_CWAI_POST = 7'd110;
localparam CPUSTATE_DMABREQ = 7'd111;
localparam CPUSTATE_DMABREQ_EXIT = 7'd112;
localparam CPUSTATE_SYNC = 7'd113;
localparam CPUSTATE_SYNC_EXIT = 7'd114;
localparam CPUSTATE_INT_DONTCARE = 7'd115;
reg [6:0] CpuState = CPUSTATE_RESET;
reg [6:0] CpuState_nxt = CPUSTATE_RESET;
reg [6:0] NextState = CPUSTATE_RESET;
reg [6:0] NextState_nxt = CPUSTATE_RESET;
wire [6:0] PostIllegalState;
// If we encounter something like an illegal addressing mode (an index mode that's illegal for instance)
// What state should we go to?
generate
if (ILLEGAL_INSTRUCTIONS=="STOP")
begin : postillegal
assign PostIllegalState = CPUSTATE_STOP;
end
else
begin
assign PostIllegalState = CPUSTATE_FETCH_I1;
end
endgenerate
///////////////////////////////////////////////////////////////////////
//
// MapInstruction - Considering how the core was instantiated, this
// will either directly return D[7:0] *or* remap values from D[7:0]
// that relate to undefined instructions in the 6809 to the instructions
// that the 6809 actually executed when these were encountered, due to
// incomplete decoding.
//
// NEG, COM, LSR, DEC - these four instructions, in Direct, Inherent (A or B)
// Indexed, or Extended addressing do not actually decode bit 0 on the instruction.
// Thus, for instance, a $51 encountered will be executed as a $50, which is a NEGB.
//
// Specifically, the input is an instruction; if it matches an unknown instruction that the
// 6809 is known to ghost to another instruction, the output of the function
// is the the instruction that actually gets executed. Otherwise, the output is the
// input.
function [7:0] MapInstruction(input [7:0] i);
reg [3:0] topnyb;
reg [3:0] btmnyb;
reg [7:0] newinst;
begin
newinst = i;
topnyb = i[7:4];
btmnyb = i[3:0];
if ( (topnyb == 4'H0) ||
(topnyb == 4'H4) ||
(topnyb == 4'H5) ||
(topnyb == 4'H6) ||
(topnyb == 4'H7)
)
begin
if (btmnyb == 4'H1)
newinst = {topnyb, 4'H0};
if (btmnyb == 4'H2)
newinst = {topnyb, 4'H3};
if (btmnyb == 4'H5)
newinst = {topnyb, 4'H4};
if (btmnyb == 4'HB)
newinst = {topnyb, 4'HA};
end
MapInstruction = newinst;
end
endfunction
wire [7:0] MappedInstruction;
generate
if (ILLEGAL_INSTRUCTIONS=="GHOST")
begin : ghost
assign MappedInstruction = MapInstruction(D);
end
else
begin
assign MappedInstruction = D;
end
endgenerate
///////////////////////////////////////////////////////////////////////
function IllegalInstruction(input [7:0] i);
reg [3:0] hi;
reg [3:0] lo;
reg illegal;
begin
illegal = 1'b0;
hi = i[7:4];
lo = i[3:0];
if ( (hi == 4'H0) || (hi == 4'H4) || (hi == 4'H5) || (hi == 4'H6) || (hi == 4'H7) )
begin
if ( (lo == 4'H1) || (lo == 4'H2) || (lo == 4'H5) || (lo == 4'HB) )
illegal = 1'b1;
if (lo == 4'HE)
if ( (hi == 4'H4) || (hi == 4'H5) )
illegal = 1'b1;
end
if (hi == 4'H3)
begin
if ( (lo == 4'H8) || (lo == 4'HE) )
illegal = 1'b1;
end
if (hi == 4'H1)
begin
if ( (lo == 4'H4) || (lo == 4'H5) || (lo == 4'H8) || (lo == 4'HB) )
illegal = 1'b1;
end
if ( (hi == 4'H8) || (hi == 4'HC) )
begin
if ( (lo == 4'H7) || (lo == 4'HF) )
illegal = 1'b1;
if ( lo == 4'HD )
if (hi == 4'HC)
illegal = 1'b1;
end
IllegalInstruction = illegal;
end
endfunction
wire IsIllegalInstruction;
generate
if (ILLEGAL_INSTRUCTIONS=="GHOST")
begin : never_illegal
assign IsIllegalInstruction = 1'b0;
end
else
begin
assign IsIllegalInstruction = IllegalInstruction(Inst1);
end
endgenerate
wire [6:0] IllegalInstructionState;
generate
if (ILLEGAL_INSTRUCTIONS=="IGNORE")
begin : illegal_state
assign IllegalInstructionState = CPUSTATE_FETCH_I1;
end
else if (ILLEGAL_INSTRUCTIONS=="STOP")
begin
assign IllegalInstructionState = CPUSTATE_STOP;
end
else
begin
assign IllegalInstructionState = 7'd0;
end
endgenerate
///////////////////////////////////////////////////////////////////////
always @(negedge NMISample2 or posedge wNMIClear)
begin
if (wNMIClear == 1)
NMILatched <= 1;
else if (NMIMask == 0)
NMILatched <= 0;
else
NMILatched <= 1;
end
//
// The 6809 specs say that the CPU control signals are sampled on the falling edge of Q.
// It also says that the interrupts require 1 cycle of synchronization time.
// That's vague, as it doesn't say where "1 cycle" starts or ends. Starting from the
// falling edge of Q, the next cycle notices an assertion. From checking a hard 6809 on
// an analyzer, what they really mean is that it's sampled on the falling edge of Q,
// but there's a one cycle delay from the falling edge of E (0.25 clocks from the falling edge of Q
// where the signals were sampled) before it can be noticed.
// So, SIGNALSample is the latched value at the falling edge of Q
// SIGNALSample2 is the latched value at the falling edge of E (0.25 clocks after the line above)
// SIGNALLatched is the latched value at the falling edge of E (1 cycle after the line above)
//
// /HALT and /DMABREQ are delayed one cycle less than interrupts. The 6809 specs infer these details,
// but don't list the point-of-reference they're written from (for instance, they say that an interrupt requires
// a cycle for synchronization; however, it isn't clear whether that's from the falling Q to the next falling Q,
// a complete intermediate cycle, the falling E to the next falling E, etc.) - which, in the end, required an
// analyzer on the 6809 to determine how many cycles before a new instruction an interrupt (or /HALT & /DMABREQ)
// had to be asserted to be noted instead of the next instruction running start to finish.
//
always @(negedge Q)
begin
NMISample <= nNMI;
IRQSample <= nIRQ;
FIRQSample <= nFIRQ;
HALTSample <= nHALT;
DMABREQSample <= nDMABREQ;
end
reg rnRESET=0; // The latched version of /RESET, useful 1 clock after it's latched
always @(negedge E)
begin
rnRESET <= nRESET;
NMISample2 <= NMISample;
IRQSample2 <= IRQSample;
IRQLatched <= IRQSample2;
FIRQSample2 <= FIRQSample;
FIRQLatched <= FIRQSample2;
HALTSample2 <= HALTSample;
HALTLatched <= HALTSample2;
DMABREQSample2 <= DMABREQSample;
DMABREQLatched <= DMABREQSample2;
if (rnRESET == 1)
begin
CpuState <= CpuState_nxt;
// Don't interpret this next item as "The Next State"; it's a special case 'after this
// generic state, go to this programmable state', so that a single state
// can be shared for many tasks. [Specifically, the stack push/pull code, which is used
// for PSH, PUL, Interrupts, RTI, etc.
NextState <= NextState_nxt;
// CPU registers latch from the combinatorial circuit
a <= a_nxt;
b <= b_nxt;
x <= x_nxt;
y <= y_nxt;
s <= s_nxt;
u <= u_nxt;
cc <= cc_nxt;
dp <= dp_nxt;
pc <= pc_nxt;
tmp <= tmp_nxt;
addr <= addr_nxt;
ea <= ea_nxt;
InstPage2 <= InstPage2_nxt;
InstPage3 <= InstPage3_nxt;
Inst1 <= Inst1_nxt;
Inst2 <= Inst2_nxt;
Inst3 <= Inst3_nxt;
NMIClear <= NMIClear_nxt;
IntType <= IntType_nxt;
if (s != s_nxt) // Once S changes at all (default is '0'), release the NMI Mask.
NMIMask <= 1'b0;
end
else
begin
CpuState <= CPUSTATE_RESET;
NMIMask <= 1'b1; // Mask NMI until S is loaded.
NMIClear <= 1'b0; // Mark us as not having serviced NMI
end
end
/////////////////////////////////////////////////////////////////
// Decode the Index byte
localparam IDX_REG_X = 3'd0;
localparam IDX_REG_Y = 3'd1;
localparam IDX_REG_U = 3'd2;
localparam IDX_REG_S = 3'd3;
localparam IDX_REG_PC = 3'd4;
localparam IDX_MODE_POSTINC1 = 4'd0;
localparam IDX_MODE_POSTINC2 = 4'd1;
localparam IDX_MODE_PREDEC1 = 4'd2;
localparam IDX_MODE_PREDEC2 = 4'd3;
localparam IDX_MODE_NOOFFSET = 4'd4;
localparam IDX_MODE_B_OFFSET = 4'd5;
localparam IDX_MODE_A_OFFSET = 4'd6;
localparam IDX_MODE_5BIT_OFFSET= 4'd7; // Special case, not bit pattern 7; the offset sits in the bit pattern
localparam IDX_MODE_8BIT_OFFSET= 4'd8;
localparam IDX_MODE_16BIT_OFFSET = 4'd9;
localparam IDX_MODE_D_OFFSET = 4'd11;
localparam IDX_MODE_8BIT_OFFSET_PC = 4'd12;
localparam IDX_MODE_16BIT_OFFSET_PC= 4'd13;
localparam IDX_MODE_EXTENDED_INDIRECT = 4'd15;
// Return:
// Register base [3 bits]
// Indirect [1 bit]
// Mode [4 bits]
function [7:0] IndexDecode(input [7:0] postbyte);
reg [2:0] regnum;
reg indirect;
reg [3:0] mode;
begin
indirect = 0;
mode = 0;
if (postbyte[7] == 0) // 5-bit
begin
mode = IDX_MODE_5BIT_OFFSET;
end
else
begin
mode = postbyte[3:0];
indirect = postbyte[4];
end
if ((mode != IDX_MODE_8BIT_OFFSET_PC) && (mode != IDX_MODE_16BIT_OFFSET_PC))
regnum[2:0] = postbyte[6:5];
else
regnum[2:0] = IDX_REG_PC;
IndexDecode = {indirect, mode, regnum};
end
endfunction
wire [3:0] IndexedMode;
wire IndexedIndirect;
wire [2:0] IndexedRegister;
assign {IndexedIndirect, IndexedMode, IndexedRegister} = IndexDecode(Inst2);
/////////////////////////////////////////////////////////////////
// Is this a JMP instruction? (irrespective of addressing mode)
function IsJMP(input [7:0] inst);
reg [3:0] hi;
reg [3:0] lo;
begin
hi = inst[7:4];
lo = inst[3:0];
IsJMP = 0;
if ((hi == 4'H0) || (hi == 4'H6) || (hi == 4'H7))
if (lo == 4'HE)
IsJMP = 1;
end
endfunction
///////////////////////////////////////////////////////////////////
// Is this an 8-bit Store?
localparam ST8_REG_A = 1'b0;
localparam ST8_REG_B = 1'b1;
function [1:0] IsST8(input [7:0] inst);
reg regnum;
reg IsStore;
begin
IsStore = 1'b0;
regnum = 1'b1;
if ( (Inst1 == 8'H97) || (Inst1 == 8'HA7) || (Inst1 == 8'HB7) )
begin
IsStore = 1'b1;
regnum = 1'b0;
end
else if ( (Inst1 == 8'HD7) || (Inst1 == 8'HE7) || (Inst1 == 8'HF7) )
begin
IsStore = 1'b1;
regnum = 1'b1;
end
IsST8 = {IsStore, regnum};
end
endfunction
wire IsStore8;
wire Store8RegisterNum;
assign {IsStore8, Store8RegisterNum} = IsST8(Inst1);
/////////////////////////////////////////////////////////////////
// Is this a 16-bit Store?
localparam ST16_REG_X = 3'd0;
localparam ST16_REG_Y = 3'd1;
localparam ST16_REG_U = 3'd2;
localparam ST16_REG_S = 3'd3;
localparam ST16_REG_D = 3'd4;
function [3:0] IsST16(input [7:0] inst);
reg [3:0] hi;
reg [3:0] lo;
reg [2:0] regnum;
reg IsStore;
begin
hi = inst[7:4];
lo = inst[3:0];
IsStore = 1'b0;
regnum = 3'b111;
if ((inst == 8'H9F) || (inst == 8'HAF) || (inst == 8'HBF))
begin
IsStore = 1;
if (~InstPage2)
regnum = ST16_REG_X;
else
regnum = ST16_REG_Y;
end
else if ((inst == 8'HDF) || (inst == 8'HEF) || (inst == 8'HFF))
begin
IsStore = 1;
if (~InstPage2)
regnum = ST16_REG_U;
else
regnum = ST16_REG_S;
end
else if ((inst == 8'HDD) || (inst == 8'HED) || (inst == 8'HFD))
begin
IsStore = 1;
regnum = ST16_REG_D;
end
IsST16 = {IsStore, regnum};
end
endfunction
wire IsStore16;
wire [2:0] StoreRegisterNum;
assign {IsStore16, StoreRegisterNum} = IsST16(Inst1);
/////////////////////////////////////////////////////////////////
// Is this a special Immediate mode instruction, ala
// PSH, PUL, EXG, TFR, ANDCC, ORCC
function IsSpecialImm(input [7:0] inst);
reg is;
reg [3:0] hi;
reg [3:0] lo;
begin
hi = inst[7:4];
lo = inst[3:0];
is = 0;
if (hi == 4'H1)
begin
if ( (lo == 4'HA) || (lo == 4'HC) || (lo == 4'HE) || (lo == 4'HF) ) // ORCC, ANDCC, EXG, TFR
is = 1;
end
else if (hi == 4'H3)
begin
if ( (lo >= 4'H3) && (lo <= 4'H7) ) // PSHS, PULS, PSHU, PULU
is = 1;
end
else
is = 0;
IsSpecialImm = is;
end
endfunction
wire IsSpecialImmediate = IsSpecialImm(Inst1);
/////////////////////////////////////////////////////////////////
// Is this a one-byte instruction? [The 6809 reads 2 bytes for every instruction, minimum (it can read more). On a one-byte, we have to ensure that we haven't skipped the PC ahead.
function IsOneByteInstruction(input [7:0] inst);
reg is;
reg [3:0] hi;
reg [3:0] lo;
begin
hi = inst[7:4];
lo = inst[3:0];
is = 1'b0;
if ( (hi == 4'H4) || (hi == 4'H5) )
is = 1'b1;
else if ( hi == 4'H1)
begin
if ( (lo == 4'H2) || (lo == 4'H3) || (lo == 4'H9) || (lo == 4'HD) )
is = 1'b1;
end
else if (hi == 4'H3)
begin
if ( (lo >= 4'H9) && (lo != 4'HC) )
is = 1'b1;
end
else
is = 1'b0;
IsOneByteInstruction = is;
end
endfunction
/////////////////////////////////////////////////////////////////
// ALU16 - Simpler than the 8 bit ALU
localparam ALU16_REG_X = 3'd0;
localparam ALU16_REG_Y = 3'd1;
localparam ALU16_REG_U = 3'd2;
localparam ALU16_REG_S = 3'd3;
localparam ALU16_REG_D = 3'd4;
function [2:0] ALU16RegFromInst(input Page2, input Page3, input [7:0] inst);
reg [2:0] srcreg;
begin
srcreg = 3'b111; // default
casex ({Page2, Page3, inst}) // Note pattern for the matching below
10'b1010xx0011: // 1083, 1093, 10A3, 10B3 CMPD
srcreg = ALU16_REG_D;
10'b1010xx1100: // 108C, 109C, 10AC, 10BC CMPY
srcreg = ALU16_REG_Y;
10'b0110xx0011: // 1183, 1193, 11A3, 11B3 CMPU
srcreg = ALU16_REG_U;
10'b0110xx1100: // 118C, 119C, 11AC, 11BC CMPS
srcreg = ALU16_REG_S;
10'b0010xx1100: // 8C,9C,AC,BC CMPX
srcreg = ALU16_REG_X;
10'b0011xx0011: // C3, D3, E3, F3 ADDD
srcreg = ALU16_REG_D;
10'b0011xx1100: // CC, DC, EC, FC LDD
srcreg = ALU16_REG_D;
10'b0010xx1110: // 8E LDX, 9E LDX, AE LDX, BE LDX
srcreg = ALU16_REG_X;
10'b0011xx1110: // CE LDU, DE LDU, EE LDU, FE LDU
srcreg = ALU16_REG_U;
10'b1010xx1110: // 108E LDY, 109E LDY, 10AE LDY, 10BE LDY
srcreg = ALU16_REG_Y;
10'b1011xx1110: // 10CE LDS, 10DE LDS, 10EE LDS, 10FE LDS
srcreg = ALU16_REG_S;
10'b0010xx0011: // 83, 93, A3, B3 SUBD
srcreg = ALU16_REG_D;
10'H03A: // 3A ABX
srcreg = ALU16_REG_X;
10'H030: // 30 LEAX
srcreg = ALU16_REG_X;
10'H031: // 31 LEAY
srcreg = ALU16_REG_Y;
10'H032: // 32 LEAS
srcreg = ALU16_REG_S;
10'H033: // 32 LEAU
srcreg = ALU16_REG_U;
default:
srcreg = 3'b111;
endcase
ALU16RegFromInst = srcreg;
end
endfunction
wire [2:0] ALU16Reg = ALU16RegFromInst(InstPage2, InstPage3, Inst1);
localparam ALUOP16_SUB = 3'H0;
localparam ALUOP16_ADD = 3'H1;
localparam ALUOP16_LD = 3'H2;
localparam ALUOP16_CMP = 3'H3;
localparam ALUOP16_LEA = 3'H4;
localparam ALUOP16_INVALID = 3'H7;
function [3:0] ALU16OpFromInst(input Page2, input Page3, input [7:0] inst);
reg [2:0] aluop;
reg writeback;
begin
aluop = 3'b111;
writeback = 1'b1;
casex ({Page2, Page3, inst})
10'b1010xx0011: // 1083, 1093, 10A3, 10B3 CMPD
begin
aluop = ALUOP16_CMP;
writeback = 1'b0;
end
10'b1010xx1100: // 108C, 109C, 10AC, 10BC CMPY
begin
aluop = ALUOP16_CMP;
writeback = 1'b0;
end
10'b0110xx0011: // 1183, 1193, 11A3, 11B3 CMPU
begin
aluop = ALUOP16_CMP;
writeback = 1'b0;
end
10'b0110xx1100: // 118C, 119C, 11AC, 11BC CMPS
begin
aluop = ALUOP16_CMP;
writeback = 1'b0;
end
10'b0010xx1100: // 8C,9C,AC,BC CMPX
begin
aluop = ALUOP16_CMP;
writeback = 1'b0;
end
10'b0011xx0011: // C3, D3, E3, F3 ADDD
aluop = ALUOP16_ADD;
10'b0011xx1100: // CC, DC, EC, FC LDD
aluop = ALUOP16_LD;
10'b001xxx1110: // 8E LDX, 9E LDX, AE LDX, BE LDX, CE LDU, DE LDU, EE LDU, FE LDU
aluop = ALUOP16_LD;
10'b101xxx1110: // 108E LDY, 109E LDY, 10AE LDY, 10BE LDY, 10CE LDS, 10DE LDS, 10EE LDS, 10FE LDS
aluop = ALUOP16_LD;
10'b0010xx0011: // 83, 93, A3, B3 SUBD
aluop = ALUOP16_SUB;
10'H03A: // 3A ABX
aluop = ALUOP16_ADD;
10'b00001100xx: // $30-$33, LEAX, LEAY, LEAS, LEAU
aluop = ALUOP16_LEA;
default:
aluop = ALUOP16_INVALID;
endcase
ALU16OpFromInst = {writeback, aluop};
end
endfunction
wire ALU16OpWriteback;
wire [2:0] ALU16Opcode;
assign {ALU16OpWriteback, ALU16Opcode} = ALU16OpFromInst(InstPage2, InstPage3, Inst1);
wire IsALU16Opcode = (ALU16Opcode != 3'b111);