forked from hoglet67/verilog-6502
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_HuC6280.v
2334 lines (1994 loc) · 62.6 KB
/
cpu_HuC6280.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
`default_nettype none
/*
* verilog model of HuC6280 CPU.
*
* Based on original 6502 "Arlet 6502 Core" by Arlet Ottens + 65C02 extension
*
* (C) Arlet Ottens, <[email protected]>
*
* Feel free to use this code in any project (commercial or not), as long as you
* keep this message, and the copyright notice. This code is provided "as is",
* without any warranties of any kind.
*
* Support for 65C02 instructions and addressing modes by David Banks and Ed
* Spittles
*
* (C) 2016 David Banks and Ed Spittles
*
* Feel free to use this code in any project (commercial or not), as long as you
* keep this message, and the copyright notice. This code is provided "as is",
* without any warranties of any kind.
*
* Support for HuC6280 instructions and addressing modes by Ford Seidel and
* Amolak Nagi
* (C) 2018 Ford Seidel and Amolak Nagi
*
* Feel free to use this code in any project (commercial or not), as long as you
* keep this message, and the copyright notice. This code is provided "as is",
* without any warranties of any kind.
*/
/*
* Note that not all 6502 interface signals are supported (yet). The goal
* is to create an Acorn Atom model, and the Atom didn't use all signals on
* the main board.
*
* The data bus is implemented as separate read/write buses. Combine them
* on the output pads if external memory is required.
*/
/*
* Two things were needed to correctly implement 65C02 NOPs
* 1. Ensure the microcode state machine uses an appropriate addressing mode for the opcode length
* 2. Ensure there are no side-effects (e.g. register updates, memory stores, etc)
*
* If IMPLEMENT_NOPS is defined, the state machine is modified accordingly.
*/
`define IMPLEMENT_NOPS
/*
* Two things were needed to correctly implement 65C02 BCD arithmentic
* 1. The Z flag needs calculating over the BCD adjusted ALU output
* 2. The N flag needs calculating over the BCD adjusted ALU output
*
* If IMPLEMENT_CORRECT_BCD_FLAGS is defined, this additional logic is added
*/
`define IMPLEMENT_CORRECT_BCD_FLAGS
//set this to get debugging aids
`define SIM
module cpu_HuC6280( clk, reset, AB_21, DO, EXT_out, RE, WE, IRQ1_n, IRQ2_n,
NMI, HSM, RDY_n, CE_n, CER_n, CE7_n, CEK_n);
input clk; // CPU clock
input reset; // reset signal
output wire [20:0] AB_21; // address bus (post-MMU)
output [7:0] DO; // data out, write bus
input [7:0] EXT_out; // data driven by external peripherals
output RE; // read enable
output WE; // write enable
input IRQ1_n; // interrupt request 1
input IRQ2_n; // interrupt request 2
input NMI; // non-maskable interrupt request
output reg HSM; // high speed mode enabled
input RDY_n; // Ready signal. Pauses CPU when RDY_n=1
output CE_n; // ROM enable signal
output CER_n; // RAM enable signal
output CE7_n; // VDC enable signal
output CEK_n; // VCE enable signal
//clocking
wire clk72_en, clk18_en;
clock_divider #(3) clk72(.clk(clk), .reset(reset), .clk_en(clk72_en));
clock_divider #(12) clk18(.clk(clk), .reset(reset), .clk_en(clk18_en));
wire clk_en;
//assign clk_en = 1; //This line is nice for testing, but BE CAREFUL
assign clk_en = (HSM) ? clk72_en : clk18_en;
wire RDY_preMMU, RDY, MMU_stall;
assign RDY_preMMU = ~RDY_n & clk_en; //cheap hack to do variable clock speed
assign RDY = RDY_preMMU & ~MMU_stall;
wire [7:0] DI; // data in, read bus (for RAM)
//wire CE7_n;
//wire CEK_n;
wire CEP_n;
wire CET_n;
wire CEIO_n;
wire CECG_n; //Interrupt controller enable
wire TIQ_n; // timer interrupt request
MAIN_RAM ram(.clock(clk), .address(AB_21[12:0]), .data(DO),
.wren(WE & ~CER_n), .q(DI));
/* //TODO: enable these
output CE_n;
output CER_n;
output CE7_n;
output CEK_n;
output CEP_n;
output CET_n;
output CEIO_n;
output CECG_n;
*/
/*
* internal signals
*/
reg [15:0] AB; // Address bus (pre-MMU)
reg [15:0] PC; // Program Counter
reg [7:0] ABL; // Address Bus Register LSB
reg [7:0] ABH; // Address Bus Register MSB
wire [7:0] ADD; // Adder Hold Register (registered in ALU)
reg [7:0] DIHOLD; // Hold for Data In
reg DIHOLD_valid; //
wire [7:0] DIMUX; //
reg DIMUX_IO; // next cycle should read from internal IO buffer
reg [7:0] IRHOLD; // Hold for Instruction register
reg IRHOLD_valid; // Valid instruction in IRHOLD
reg [7:0] AXYS[3:0]; // A, X, Y and S register file
reg C = 0; // carry flag (init at zero to avoid X's in ALU sim)
reg Z = 0; // zero flag
reg I = 0; // interrupt flag
reg D = 0; // decimal flag
reg V = 0; // overflow flag
reg T = 0; // T flag
reg N = 0; // negative flag
wire AZ; // ALU Zero flag
wire AZ1; // ALU Zero flag (BCD adjusted)
reg AZ2; // ALU Second Zero flag, set using TSB/TRB semantics
wire AV; // ALU overflow flag
wire AN; // ALU negative flag
wire AN1; // ALU negative flag (BCD adjusted)
wire HC; // ALU half carry
reg [7:0] AI; // ALU Input A
reg [7:0] BI; // ALU Input B
wire [7:0] IR; // Instruction register
reg [7:0] DO; // Data Out
wire [7:0] AO; // ALU output after BCD adjustment
reg RE; // Read Enable
reg WE; // Write Enable
reg CI; // Carry In
wire CO; // Carry Out
wire [7:0] PCH = PC[15:8];
wire [7:0] PCL = PC[7:0];
reg bbx_status; // a cheap hack to make my life easier (fseidel)
reg [7:0] bbx_disp; // ditto
reg [7:0] tst_mask; // I'm starting to think these aren't hacks
reg [7:0] bsr_disp; // hrm...
reg NMI_edge = 0; // captured NMI edge
reg [1:0] regsel; // Select A, X, Y or S register
wire [7:0] regfile = AXYS[regsel]; // Selected register output
parameter
SEL_A = 2'd0,
SEL_S = 2'd1,
SEL_X = 2'd2,
SEL_Y = 2'd3;
/*
* define some signals for watching in simulator output
*/
`ifdef SIM
wire [7:0] A = AXYS[SEL_A]; // Accumulator
wire [7:0] X = AXYS[SEL_X]; // X register
wire [7:0] Y = AXYS[SEL_Y]; // Y register
wire [7:0] S = AXYS[SEL_S]; // Stack pointer
`endif
wire [7:0] P = { N, V, T, 1'b1, D, I, Z, C };
/*
* instruction decoder/sequencer
*/
reg [6:0] state;
/*
* control signals
*/
reg PC_inc; // Increment PC
reg [15:0] PC_temp; // intermediate value of PC
reg [1:0] src_reg; // source register index
reg [1:0] dst_reg; // destination register index
reg index_y; // if set, then Y is index reg rather than X
reg load_reg; // loading a register (A, X, Y, S) in this instruction
reg inc; // increment
reg write_back; // set if memory is read/modified/written
reg load_only; // LDA/LDX/LDY instruction
reg store; // doing store (STA/STX/STY)
reg adc_sbc; // doing ADC/SBC
reg compare; // doing CMP/CPY/CPX
reg shift; // doing shift/rotate instruction
reg rotate; // doing rotate (no shift)
reg backwards; // backwards branch
reg cond_true; // branch condition is true
reg [3:0] cond_code; // condition code bits from instruction
reg shift_right; // Instruction ALU shift/rotate right
reg alu_shift_right; // Current cycle shift right enable
reg [3:0] op; // Main ALU operation for instruction
reg [3:0] alu_op; // Current cycle ALU operation
reg [2:0] mask_shift; // bit select for RMB/SMB instructions
reg adc_bcd; // ALU should do BCD style carry
reg adj_bcd; // results should be BCD adjusted
/*
* some flip flops to remember we're doing special instructions. These
* get loaded at the DECODE state, and used later
*/
reg store_zero; // doing STZ instruction
reg trb_ins; // doing TRB instruction
reg txb_ins; // doing TSB/TRB instruction
reg rmb_ins; // doing RMB instruction
reg xmb_ins; // doing SMB/RMB instruction
reg bbx_ins; // doing BBS/BBR instruction
reg bbr_ins; // doing BBR instruction
reg bit_ins; // doing BIT instruction
reg bit_ins_nv; // doing BIT instruction that will update the n and v
// flags (i.e. not BIT imm)
reg txx_ins; // doing transfer instruction (COMBINATIONAL!)
reg tii_ins; // doing TII instruction
reg tdd_ins; // doing TDD instruction
reg tin_ins; // doing TIN instruction
reg tia_ins; // doing TIA instruction
reg tai_ins; // doing TAI instruction
reg swp_ins; // doing SXY, SAX, SAY instruction
reg sax_ins; // doing SAX instruction
reg clr_ins; // doing CLA/CLX/CLY instruction
reg tst_ins; // doing TST instruction
reg tst_x; // TST instruction is x-relative
reg [1:0] stx_dst; // destination of an ST{0,1,2} instruction
reg plp; // doing PLP instruction
reg php; // doing PHP instruction
reg clc; // clear carry
reg sec; // set carry
reg cld; // clear decimal
reg sed; // set decimal
reg cli; // clear interrupt
reg sei; // set interrupt
reg clv; // clear overflow
reg brk; // doing BRK
reg res; // in reset
wire IRQ, IRQ1, IRQ2, TIQ;
assign IRQ = IRQ1 | IRQ2 | TIQ; //global signal to indicate presence of IRQ
/*
//IRQ debug
always @(posedge IRQ) begin
if(IRQ) begin
$stop;
end
end
*/
always @(posedge IRQ) begin
$display("IRQ triggered!");
//$stop;
end
/*
* DIMUX handling
* TODO: handle cases where I/O buffer is not written
*/
wire [7:0] INT_out, TIMER_out, cur_read;
reg [7:0] IO_out, latched_read;
reg read_delay; //selects whether or not we go for a real read on next clock
/*
always @(posedge clk) begin
if(reset)
read_delay <= 0;
else if(RDY)
read_delay <= 0;
else
read_delay <= 1;
end
*/
assign cur_read = (DIMUX_IO) ? IO_out : DI;
//assign DIMUX = (read_delay) ? latched_read : cur_read;
assign DIMUX = latched_read;
//only latch reads when we are mid-cycle
always @(posedge clk) begin
if( RDY )
latched_read <= cur_read;
end
wire [7:0] PAD_out;
assign PAD_out = 8'b1011_1111; // Region bit == Japan
wire IO_sel; //will be set by MMU
always @* begin
IO_out = 8'hxx;
DIMUX_IO = 0;
if(RE) begin
if(~CECG_n) begin //interrupt controller
IO_out = INT_out;
DIMUX_IO = 1;
end
else if (~CET_n) begin //timer
IO_out = TIMER_out;
DIMUX_IO = 1;
end
else if(~CEIO_n) begin //controller/IO port
IO_out = PAD_out;
DIMUX_IO = 1;
end
else if(~CE_n) begin //ROM
IO_out = EXT_out;
DIMUX_IO = 1;
end
else if(IO_sel) begin //external peripherals
IO_out = EXT_out;
DIMUX_IO = 1;
end
end
end
/*
* Interrupt controller
*/
wire TIQ_ack;
INT_ctrl ictrl(.clk(clk), .reset(reset), .RDY(RDY), .re(RE), .we(WE),
.CECG_n(CECG_n), .addr(AB_21[1:0]),
.dIn(DO), .dOut(INT_out),
.TIQ_n(TIQ_n), .IRQ1_n(IRQ1_n), .IRQ2_n(IRQ2_n),
.TIQ(TIQ), .IRQ1(IRQ1), .IRQ2(IRQ2),
.TIQ_ack(TIQ_ack));
/*
* Timer
*/
TIMER itimer(.clk(clk), .reset(reset), //stupid name because of keywords
.re(RE), .we(WE),
.clk_en(clk72_en), .dIn(DO), .dOut(TIMER_out),
.CET_n(CET_n), .addr(AB_21[0]), .TIQ_ack(TIQ_ack), .TIQ_n(TIQ_n));
/*
* Block transfer bookkeeping
*/
reg [15:0] txx_src;
reg [15:0] txx_dst;
reg [15:0] txx_len;
reg txx_alt;
/*
* ALU operations
*/
parameter
OP_OR = 4'b1100,
OP_AND = 4'b1101,
OP_EOR = 4'b1110,
OP_ADD = 4'b0011,
OP_SUB = 4'b0111,
OP_ROL = 4'b1011,
OP_A = 4'b1111;
/*
* Microcode state machine. Basically, every addressing mode has its own
* path through the state machine. Additional information, such as the
* operation, source and destination registers are decoded in parallel, and
* kept in separate flops.
*/
parameter
ABS0 = 7'd0, // ABS - fetch LSB
ABS1 = 7'd1, // ABS - fetch MSB
ABSX0 = 7'd2, // ABS, X - fetch LSB and send to ALU (+X)
ABSX1 = 7'd3, // ABS, X - fetch MSB and send to ALU (+Carry)
ABSX2 = 7'd4, // ABS, X - Wait for ALU (only if needed)
BRA0 = 7'd5, // Branch - fetch offset and send to ALU (+PC[7:0])
BRA1 = 7'd6, // Branch - fetch opcode, and send PC[15:8] to ALU
BRA2 = 7'd7, // Branch - fetch opcode (if page boundary crossed)
BRK0 = 7'd8, // BRK/IRQ - push PCH, send S to ALU (-1)
BRK1 = 7'd9, // BRK/IRQ - push PCL, send S to ALU (-1)
BRK2 = 7'd10, // BRK/IRQ - push P, send S to ALU (-1)
BRK3 = 7'd11, // BRK/IRQ - write S, and fetch @ fffe
DECODE = 7'd12, // IR is valid, decode instruction, and write prev reg
FETCH = 7'd13, // fetch next opcode, and perform prev ALU op
INDX0 = 7'd14, // (ZP,X) - fetch ZP address, and send to ALU (+X)
INDX1 = 7'd15, // (ZP,X) - fetch LSB at ZP+X, calculate ZP+X+1
INDX2 = 7'd16, // (ZP,X) - fetch MSB at ZP+X+1
INDX3 = 7'd17, // (ZP,X) - fetch data
INDY0 = 7'd18, // (ZP),Y - fetch ZP address, and send ZP to ALU (+1)
INDY1 = 7'd19, // (ZP),Y - fetch at ZP+1, and send LSB to ALU (+Y)
INDY2 = 7'd20, // (ZP),Y - fetch data, and send MSB to ALU (+Carry)
INDY3 = 7'd21, // (ZP),Y) - fetch data (if page boundary crossed)
JMP0 = 7'd22, // JMP - fetch PCL and hold
JMP1 = 7'd23, // JMP - fetch PCH
JMPI0 = 7'd24, // JMP IND - fetch LSB and send to ALU for delay (+0)
JMPI1 = 7'd25, // JMP IND - fetch MSB, proceed with JMP0 state
JSR0 = 7'd26, // JSR - push PCH, save LSB, send S to ALU (-1)
JSR1 = 7'd27, // JSR - push PCL, send S to ALU (-1)
JSR2 = 7'd28, // JSR - write S
JSR3 = 7'd29, // JSR - fetch MSB
PULL0 = 7'd30, // PLP/PLA/PLX/PLY - save next op in IRHOLD, send S to ALU (+1)
PULL1 = 7'd31, // PLP/PLA/PLX/PLY - fetch data from stack, write S
PULL2 = 7'd32, // PLP/PLA/PLX/PLY - prefetch op, but don't increment PC
PUSH0 = 7'd33, // PHP/PHA/PHX/PHY - send A to ALU (+0)
PUSH1 = 7'd34, // PHP/PHA/PHX/PHY - write A/P, send S to ALU (-1)
READ = 7'd35, // Read memory for read/modify/write (INC, DEC, shift)
REG = 7'd36, // Read register for reg-reg transfers
RTI0 = 7'd37, // RTI - send S to ALU (+1)
RTI1 = 7'd38, // RTI - read P from stack
RTI2 = 7'd39, // RTI - read PCL from stack
RTI3 = 7'd40, // RTI - read PCH from stack
RTI4 = 7'd41, // RTI - read PCH from stack
RTS0 = 7'd42, // RTS - send S to ALU (+1)
RTS1 = 7'd43, // RTS - read PCL from stack
RTS2 = 7'd44, // RTS - write PCL to ALU, read PCH
RTS3 = 7'd45, // RTS - load PC and increment
WRITE = 7'd46, // Write memory for read/modify/write
ZP0 = 7'd47, // Z-page - fetch ZP address
ZPX0 = 7'd48, // ZP, X - fetch ZP, and send to ALU (+X)
ZPX1 = 7'd49, // ZP, X - load from memory
IND0 = 7'd50, // (ZP) - fetch ZP address, and send to ALU (+0)
JMPIX0 = 7'd51, // JMP (,X)- fetch LSB and send to ALU (+X)
JMPIX1 = 7'd52, // JMP (,X)- fetch MSB and send to ALU (+Carry)
JMPIX2 = 7'd53, // JMP (,X)- Wait for ALU (only if needed)
/**
BBX0 = 7'd54, // BB{R,S} - increment PC
BBX1 = 7'd55, // BB{R,S} - fetch ZP data and send to ALU, test
BBX2 = 7'd56, // BB{R,S} - do nothing
BBX3 = 7'd57, // BB{R,S} - fetch displacement, possibly goto fetch
BBX4 = 7'd58, // BB{R,S} - add displacement to PC[7:0] (PC++????)
BBX5 = 7'd59; // BB{R,S} - add carry to PC[15:8], goto fetch
*/
//going to take liberties with bus cycles for now
BBX0 = 7'd54, // BB{R,S} - fetch ZP data
BBX1 = 7'd55, // BB{R,S} - test, fetch displacement
BBX2 = 7'd56, // BB{R,S} - write displacement to temp, PC++
BBX3 = 7'd57, // BB{R,S} - add displacement to PC[7:0]
BBX4 = 7'd58, // BB{R,S} - add carry to PC[15:8]
BBX5 = 7'd59, // BB{R,S} - set up address bus
//TXX - transfer instructions
TXX0 = 7'd60, //SP->ALU
TXX1 = 7'd61, //PUSH Y
TXX2 = 7'd62, //PUSH A
TXX3 = 7'd63, //PUSH X
TXX4 = 7'd64, //SRC[7:0], PC++
TXX5 = 7'd65, //SRC[15:8], PC++
TXX6 = 7'd66, //DST[7:0], PC++
TXX7 = 7'd67, //DST[15:0], PC++
TXX8 = 7'd68, //LEN[7:0], PC++
TXX9 = 7'd69, //LEN[15:0]
TXXA = 7'd70, //(read finishes), txx_alt = 0
TXXB = 7'd71, //setup SRC address
TXXC = 7'd72, //read SRC byte
TXXD = 7'd73, //setup DST address, write back read DATA to X
TXXE = 7'd74, //write x to DST
TXXF = 7'd75, //modify SRC, LEN--
TXXG = 7'd76, //modify DST, compare LEN to 0, txx_alt = ~alt
TXXH = 7'd77, //SP->ALU, PC++
TXXI = 7'd78, //POP X
TXXJ = 7'd79, //POP A
TXXK = 7'd80, //POP Y
TAM0 = 7'd81, //issue load request to MMU
TAM1 = 7'd82, //wait for MMU update
TAM2 = 7'd83, //keep waiting
TMA0 = 7'd84, //issue store request to MMU
TMA1 = 7'd85, //get result
SWP = 7'd86, //S{AX,AY,XY}
IMZP0 = 7'd87, //fetch zp offset
IMZP1 = 7'd88, //add offset to X or 0
IMZP2 = 7'd89, //wait
IMZP3 = 7'd90, //wait (this and next line are SWAPPED on real CPU)
IMZP4 = 7'd91, //read zp byte
IMAB0 = 7'd92, //read low address byte
IMAB1 = 7'd93, //read high address byte, DIMUX->ALU (+0/X)
IMAB2 = 7'd94, //ADD->ABL, DIMUX->ALU (+0+CO)
IMAB3 = 7'd95, //ADD->ABH
IMAB4 = 7'd96, //NOP
IMAB5 = 7'd97, //read
CSX = 7'd98, //NOP cycle for CSL/CSH
BSR0 = 7'd99, //store offset to internal buffer, S->ALU
BSR1 = 7'd100,//push PCH, S--
BSR2 = 7'd101,//push PCL S--
BSR3 = 7'd102,//add offset to PCL, write S
BSR4 = 7'd103,//carry to PCH
BSR5 = 7'd104,//present PC to bus
STX0 = 7'd105,//fetch immediate
STX1 = 7'd106,//write to VDC
TFL0 = 7'd107,//read from zeropage
TFL1 = 7'd108,//do math
TFL2 = 7'd109,//bcd adjust
TFL3 = 7'd110;//write back to zeropage
`ifdef SIM
/*
* easy to read names in simulator output
*/
reg [8*7-1:0] statename;
always @*
case( state )
DECODE: statename = "DECODE";
REG: statename = "REG";
ZP0: statename = "ZP0";
ZPX0: statename = "ZPX0";
ZPX1: statename = "ZPX1";
ABS0: statename = "ABS0";
ABS1: statename = "ABS1";
ABSX0: statename = "ABSX0";
ABSX1: statename = "ABSX1";
ABSX2: statename = "ABSX2";
IND0: statename = "IND0";
INDX0: statename = "INDX0";
INDX1: statename = "INDX1";
INDX2: statename = "INDX2";
INDX3: statename = "INDX3";
INDY0: statename = "INDY0";
INDY1: statename = "INDY1";
INDY2: statename = "INDY2";
INDY3: statename = "INDY3";
READ: statename = "READ";
WRITE: statename = "WRITE";
FETCH: statename = "FETCH";
PUSH0: statename = "PUSH0";
PUSH1: statename = "PUSH1";
PULL0: statename = "PULL0";
PULL1: statename = "PULL1";
PULL2: statename = "PULL2";
JSR0: statename = "JSR0";
JSR1: statename = "JSR1";
JSR2: statename = "JSR2";
JSR3: statename = "JSR3";
RTI0: statename = "RTI0";
RTI1: statename = "RTI1";
RTI2: statename = "RTI2";
RTI3: statename = "RTI3";
RTI4: statename = "RTI4";
RTS0: statename = "RTS0";
RTS1: statename = "RTS1";
RTS2: statename = "RTS2";
RTS3: statename = "RTS3";
BRK0: statename = "BRK0";
BRK1: statename = "BRK1";
BRK2: statename = "BRK2";
BRK3: statename = "BRK3";
BRA0: statename = "BRA0";
BRA1: statename = "BRA1";
BRA2: statename = "BRA2";
JMP0: statename = "JMP0";
JMP1: statename = "JMP1";
JMPI0: statename = "JMPI0";
JMPI1: statename = "JMPI1";
JMPIX0: statename = "JMPIX0";
JMPIX1: statename = "JMPIX1";
JMPIX2: statename = "JMPIX2";
BBX0: statename = "BBX0";
BBX1: statename = "BBX1";
BBX2: statename = "BBX2";
BBX3: statename = "BBX3";
BBX4: statename = "BBX4";
BBX5: statename = "BBX5";
TXX0: statename = "TXX0";
TXX1: statename = "TXX1";
TXX2: statename = "TXX2";
TXX3: statename = "TXX3";
TXX4: statename = "TXX4";
TXX5: statename = "TXX5";
TXX6: statename = "TXX6";
TXX7: statename = "TXX7";
TXX8: statename = "TXX8";
TXX9: statename = "TXX9";
TXXA: statename = "TXXA";
TXXB: statename = "TXXB";
TXXC: statename = "TXXC";
TXXD: statename = "TXXD";
TXXE: statename = "TXXE";
TXXF: statename = "TXXF";
TXXG: statename = "TXXG";
TXXH: statename = "TXXH";
TXXI: statename = "TXXI";
TXXJ: statename = "TXXJ";
TXXK: statename = "TXXK";
TAM0: statename = "TAM0";
TAM1: statename = "TAM1";
TAM2: statename = "TAM2";
TMA0: statename = "TMA0";
TMA1: statename = "TMA1";
SWP: statename = "SWP";
IMZP0: statename = "IMZP0";
IMZP1: statename = "IMZP1";
IMZP2: statename = "IMZP2";
IMZP3: statename = "IMZP3";
IMZP4: statename = "IMZP4";
IMAB0: statename = "IMAB0";
IMAB1: statename = "IMAB1";
IMAB2: statename = "IMAB2";
IMAB3: statename = "IMAB3";
IMAB4: statename = "IMAB4";
IMAB5: statename = "IMAB5";
CSX: statename = "CSX";
BSR0: statename = "BSR0";
BSR1: statename = "BSR1";
BSR2: statename = "BSR2";
BSR3: statename = "BSR3";
BSR4: statename = "BSR4";
BSR5: statename = "BSR5";
STX0: statename = "STX0";
STX1: statename = "STX1";
TFL0: statename = "TFL0";
TFL1: statename = "TFL1";
TFL2: statename = "TFL2";
TFL3: statename = "TFL3";
default: statename = "ILLEGAL";
endcase
//always @( PC )
// $display( "%t, PC:%04x IR:%02x A:%02x X:%02x Y:%02x S:%02x C:%d Z:%d V:%d N:%d P:%02x", $time, PC, IR, A, X, Y, S, C, Z, V, N, P );
`endif
/*
* Program Counter Increment/Load. First calculate the base value in
* PC_temp.
*/
always @*
case( state )
DECODE: if( (~I & IRQ) | NMI_edge )
PC_temp = { ABH, ABL };
else
PC_temp = PC;
JMP1,
JMPI1,
JMPIX1,
JSR3,
RTS3,
RTI4: PC_temp = { DIMUX, ADD };
BRA1,
BBX4,
BSR4: PC_temp = { ABH, ADD };
JMPIX2,
BRA2,
BBX5,
BSR5: PC_temp = { ADD, PCL };
BSR0: PC_temp = { ABH, ABL };
BRK2: PC_temp = res ? 16'hfffe : //IRQ2 and BRK
NMI_edge ? 16'hfffc : //share a vector
TIQ ? 16'hfffa :
IRQ1 ? 16'hfff8 : 16'hfff6;
default: PC_temp = PC;
endcase
/*
* Determine wether we need PC_temp, or PC_temp + 1
*/
always @*
case( state ) //TODO: do txx crap with AB, not PC (maybe this is okay?)
DECODE: if( (~I & IRQ) | NMI_edge | txx_ins)
PC_inc = 0;
else
PC_inc = 1;
ABS0,
JMPIX0,
JMPIX2,
ABSX0,
FETCH,
BRA0,
BRA2,
BRK3,
JMPI1,
JMP1,
RTI4,
RTS3,
BBX2,
TXX4,
TXX5,
TXX6,
TXX7,
TXX8,
TXXH,
IMZP0,
IMAB0,
IMAB1,
BSR2: PC_inc = 1;
JMPIX1: PC_inc = ~CO; // Don't increment PC if we are
// going to go through JMPIX2
BRA1: PC_inc = CO ^~ backwards;
default: PC_inc = 0;
endcase
/*
* Set new PC
*/
always @(posedge clk)
if( RDY )
PC <= PC_temp + PC_inc;
/*
* MMU
*/
reg MMU_tam, MMU_tma;
wire STx_override;
wire [7:0] MMU_out;
assign STx_override = (state == STX1);
MMU mmu(.clk(clk), .reset(reset),
.RDY(RDY_preMMU), .load_en(MMU_tam), .store_en(MMU_tma),
.RE(RE), .WE(WE), .MMU_stall(MMU_stall),
.MPR_mask(DIMUX), .d_in(regfile), .VADDR(AB),
.STx_override(STx_override),
.PADDR(AB_21), .d_out(MMU_out),
.CE7_n(CE7_n), .CEK_n(CEK_n), .CEP_n(CEP_n), .CET_n(CET_n),
.CEIO_n(CEIO_n), .CECG_n(CECG_n), .CE_n(CE_n), .CER_n(CER_n),
.IO_sel(IO_sel));
always @* begin
MMU_tam = 0;
MMU_tma = 0;
case( state )
TAM0: MMU_tam = 1;
TMA0: MMU_tma = 1;
default: begin
MMU_tam = 0;
MMU_tma = 0;
end
endcase
end
/*
* Address Generator
*/
parameter
ZEROPAGE = 8'h20,
STACKPAGE = 8'h21;
always @*
case( state )
JMPIX1,
ABSX1,
INDX3,
INDY2,
JMP1,
JMPI1,
RTI4,
ABS1: AB = { DIMUX, ADD };
BRA2,
INDY3,
JMPIX2,
ABSX2,
IMAB3: AB = { ADD, ABL };
BRA1,
BBX4,
IMAB2: AB = { ABH, ADD };
JSR0,
PUSH1,
RTS0,
RTI0,
BRK0: AB = { STACKPAGE, regfile };
BRK1,
JSR1,
PULL1,
RTS1,
RTS2,
RTI1,
RTI2,
RTI3,
BRK2,
TXX1,
TXX2,
TXX3,
TXXI,
TXXJ,
TXXK,
BSR1,
BSR2: AB = { STACKPAGE, ADD };
INDY1,
INDX1,
ZPX1,
INDX2,
IMZP4: AB = { ZEROPAGE, ADD };
ZP0,
INDY0,
BBX0: AB = { ZEROPAGE, DIMUX };
REG,
READ,
WRITE,
SWP,
CSX,
BSR0,
IMAB4,
IMAB5: AB = { ABH, ABL };
TXXB,
TXXC: AB = txx_src;
TXXD,
TXXE: AB = txx_dst;
STX1: AB = {8'h00, 2'b00, stx_dst};
TFL0,
TFL3: AB = { ZEROPAGE, regfile };
default: AB = PC;
endcase
/*
* ABH/ABL pair is used for registering previous address bus state.
* This can be used to keep the current address, freeing up the original
* source of the address, such as the ALU or DI.
*/
always @(posedge clk)
if( state != PUSH0 && state != PUSH1 && RDY &&
state != PULL0 && state != PULL1 && state != PULL2 )
begin
ABL <= AB[7:0];
ABH <= AB[15:8];
end
/*
* Data Out MUX
*/
always @*
case( state )
WRITE,
STX1,
TFL3: DO = ADD;
JSR0,
BRK0,
BSR1: DO = PCH;
JSR1,
BRK1,
BSR2: DO = PCL;
PUSH1: DO = php ? P : ADD;
BRK2: DO = (IRQ | NMI_edge) ? (P & 8'b1110_1111) : P;
TXX1,
TXX2,
TXX3,
TXXE: DO = regfile;
default: DO = store_zero ? 0 : regfile;
endcase
/*
* Read Enable Generator. Any MMIO issues should be resolved here
*/
always @* begin
case( state )
TXXB,
TXXD,
TXXH,
IMAB3,
IMAB4: RE = 0;
default: RE = ~WE;
endcase
/*
case( state )
INDX3,
INDY3,
//ABSX2,
ABS1,
ZPX1,
ZP0: WE = ~store;
ABS0,
//ABS1,
ABSX0,
ABSX1,
BRA0,
BRA1,
BRA2,
BRK3,
DECODE,
FETCH,
INDX0,
INDX1,
INDX2,
//INDX3,
INDY0,
INDY1,
INDY2,
//INDY3,
JMP0, //investigate
JMP1,
JMPI0,
JMPI1,
JSR2,
JSR3,
PULL0,
PULL1,
PULL2,
READ,
RTI1,
RTI2,
RTI3,
RTI4,
RTS1,
RTS2,
ZP0,
ZPX0,
//ZPX1,
IND0,
JMPIX0,
JMPIX1,
JMPIX2,
BBX0,
BBX1,
BBX2,
TXX4,
TXX5,
TXX6,
TXX7,
TXX8,
TXX9,
TXXC,
TXXI,
TXXJ,
TXXK: RE = 1;
default: RE = 0;
endcase
*/
end
/*
* Write Enable Generator
*/
always @*