forked from dovoto/FPGA_Gameboy_Watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledblink.sv
1727 lines (1397 loc) · 40.3 KB
/
ledblink.sv
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
//---------------------------------------
//very simple single mode (640x480x60hz) vga
//generator appropriately named: ledblink
module ledblink
//---------------------------------------
(led, button, switches, clock_50mhz,
r, g, b, vs, hs, vblank, hblank, clock_vga,
i2c_sclk, i2c_sdata, dac_mute, dac_mclk, dac_LR_clk, dac_data, dac_bclk,
rst_button,
snes_clk, snes_data, snes_latch,
uart_tx, uart_rx,
mipi_pwm, mipi_rgb, mipi_cm, mipi_rd, mipi_cs, mipi_shut, mipi_wr, mipi_dc, mipi_rst, mipi_vddio_ctrl
);
output [3:0] led;
output [7:0] r;
output [7:0] g;
output [7:0] b;
output hs;
output vs;
output hblank;
output vblank;
output clock_vga;
output snes_clk;
output snes_latch;
input snes_data;
output uart_tx;
input uart_rx;
input [3:0] button;
input [3:0] switches;
input clock_50mhz;
input rst_button;
output i2c_sclk;
inout i2c_sdata;
output dac_mute;
output dac_mclk;
output dac_bclk;
output dac_LR_clk;
output dac_data;
output mipi_pwm;
output [7:0] mipi_rgb;
output mipi_cm;
output mipi_rd;
output mipi_cs;
output mipi_shut;
output mipi_wr;
output mipi_dc;
output mipi_rst;
output mipi_vddio_ctrl;
reg [15:0] snes_buttons;
reg [1:0] snes_state;
reg [4:0] snes_ctr;
reg [15:0] snes_shift_reg;
reg [3:0] led;
wire clock_50mhz;
reg [7:0] r;
reg [7:0] g;
reg [7:0] b;
reg hs;
reg vs;
wire hblank;
wire vblank;
wire clock_vga;
wire i2c_sclk;
wire i2c_sdata;
wire dac_mute;
wire dac_mclk;
wire dac_bclk;
wire dac_LR_clk;
wire dac_data;
reg gbc;
//----------------------------------------
wire clock_20mhz;
wire clock_108;
wire pixel_clock;
wire memory_clock;
reg half_clock;
wire full_clock;
wire cpu_clock;
wire snes_clock;
reg hs_;
reg vs_;
//vga pixel counters
wire [12:0] x;
wire [12:0] y;
wire _vblank;
wire _hblank;
wire locked;
wire reset;
wire rst;
//-------------------------------------------
//
// clock logic
//
//-------------------------------------------
assign cpu_clock = full_clock;//speed_double ? full_clock : half_clock;
assign pixel_clock = full_clock;
assign memory_clock = full_clock;
always @(posedge full_clock)
begin
half_clock = ~half_clock;
end
//-------------------------------------------
//
// uart
//
//-------------------------------------------
wire uart_clk;
wire uart_data_rdy;
wire [7:0] uart_data_rcv;
wire uart_rst_req;
wire [27:0] uart_load_addr;
wire uart_load_we;
wire uart_loading;
uart load_uart(
.rst(rst),
.clk(uart_clk),
.data_clk(cpu_clock),
.send(cpu_we && cpu_addr_bus == 16'hFF01),
.data_send(cpu_data_bus_out),
.data_rcv(uart_data_rcv),
.rdy(uart_data_rdy),
.ack(cpu_we && cpu_addr_bus == 16'hFF08),
.tx(uart_tx),
.rx(uart_rx),
.rst_req(uart_rst_req),
.load_addr(uart_load_addr),
.load_we(uart_load_we),
.loading(uart_loading));
//-------------------------------------------
//
// Frame buffer
//
//-------------------------------------------
reg [1:0] fb_data_in;
reg [15:0] fb_data_out;
reg [15:0] fb_write_addr;
reg [15:0] fb_read_addr;
reg fb_we;
frame_buffer fb(
mipi_color,
fb_read_addr,
clock_vga,
fb_write_addr,
pixel_clock,
fb_we,
fb_data_out);
//-------------------------------------------
//
// Mipi driver
//
//-------------------------------------------
reg [7:0] mipi_pwm_duty;
reg [2:0] mipi_state;
reg [7:0] mipi_data_out;
reg [7:0] mipi_control_out;
reg [8:0] mipi_x;
reg [2:0] mipi_wr_state;
reg [15:0] mipi_color;
reg [1:0] mipi_x_toggle; //scalling
reg [2:0] mipi_y_toggle; //scalling
reg mipi_line_toggle; //scalling
reg [7:0] mipi_index;
reg [7:0] mipi_cmd;
reg [7:0] mipi_y;
reg [15:0] mipi_data_out1;
reg [15:0] mipi_data_out2;
reg [7:0] mipi_line_buffer_wr_addr;
mipi_line_buffer mipi_line_buffer1(
.data({mipi_color[4:0], mipi_color[9:5], 1'b0, mipi_color[14:10]}),
.rdaddress(mipi_index),
.rdclock(clock_20mhz),
.wraddress(mipi_line_buffer_wr_addr),
.wrclock(pixel_clock),
.wren(fb_we & mipi_line_toggle),
.q(mipi_data_out1)
);
mipi_line_buffer mipi_line_buffer2(
.data({mipi_color[4:0], mipi_color[9:5], 1'b0, mipi_color[14:10]}),
.rdaddress(mipi_index),
.rdclock(clock_20mhz),
.wraddress(mipi_line_buffer_wr_addr),
.wrclock(pixel_clock),
.wren(fb_we & ~mipi_line_toggle),
.q(mipi_data_out2)
);
always @(posedge cpu_clock)
begin
if(rst) begin
mipi_pwm_duty = 8'h3f;
end else begin
if(cpu_we && cpu_addr_bus == 16'hff84) begin
mipi_data_out = cpu_data_bus_out;
end else if (cpu_we && cpu_addr_bus == 16'hff85 && unloaded) begin
mipi_control_out = cpu_data_bus_out;
end
mipi_pwm_duty = {mipi_pwm_duty[0],mipi_pwm_duty[7:1]};
mipi_pwm = mipi_pwm_duty[7];
end
end
always @(posedge clock_20mhz)
begin
if(rst) begin
mipi_state = 0;
mipi_x = 0;
mipi_wr_state = 0;
mipi_line_toggle = 0;
mipi_index = 2;
mipi_y = 0;
mipi_cmd = 8'h0;
end else begin
case (mipi_state)
0: begin //resetting
{mipi_rd, mipi_wr, mipi_cs, mipi_dc, mipi_rst, mipi_cm, mipi_shut, mipi_vddio_ctrl} = {mipi_control_out[7:3], 1'b0, 1'b0, 1'b1};
mipi_rgb = mipi_data_out;
mipi_state = (~unloaded && (pixel_y != 8'd145));
end
1: begin
if(~Reg_LCDcontrol_ff40[7] && mipi_cmd != 8'h23) begin
mipi_cmd = 8'h23;
mipi_state = 3'd2;
end else if (mipi_cmd == 8'h23 ) begin
mipi_cmd = 8'h13;
mipi_state = 3'd2;
end else begin
mipi_state = (render_mode == 2'd0 ? 3'd2 : 3'd1) ;
mipi_index = 2;
mipi_x = 9'd0;
mipi_x_toggle = 0;
mipi_cmd = (pixel_y == 9'd0 ? 8'h2C : 8'h3C);
if(render_mode == 2'd0) begin
mipi_line_toggle = mipi_line_toggle ^ 1'b1;
end
end
mipi_wr_state = 0;
end
2:begin //write start byte
mipi_y = mipi_y + 1'b1;
case(mipi_wr_state)
0: begin
mipi_rgb = mipi_cmd;
mipi_wr = 1;
mipi_cs = 1;
mipi_dc = 0;
mipi_rd = 1;
end
1: begin
mipi_wr = 1;
mipi_cs = 0;
mipi_dc = 0;
mipi_rd = 1;
end
2: begin
mipi_wr = 0;
mipi_cs = 0;
mipi_dc = 0;
mipi_rd = 1;
end
3: begin
mipi_wr = 1;
mipi_cs = 1;
mipi_dc = 1;
mipi_rd = 1;
end
4: begin
mipi_wr = 1;
mipi_cs = 0;
mipi_dc = 1;
mipi_rd = 1;
end
default: begin end
endcase
mipi_wr_state = mipi_wr_state + 1'b1;
if(~|mipi_wr_state) begin
mipi_state = (mipi_cmd == 8'd23 || mipi_cmd == 8'd13) ? 3'd1 : 3'd3;
end
mipi_x = 0;
mipi_index = 2;
end
3: begin
case(mipi_wr_state[1:0])
0: begin
mipi_rgb = mipi_line_toggle ? mipi_data_out2[7:0] : mipi_data_out1[7:0];
mipi_wr = 0;
mipi_cs = 0;
mipi_dc = 1;
mipi_rd = 1;
end
1: begin
mipi_wr = 1;
mipi_cs = 0;
mipi_dc = 1;
mipi_rd = 1;
end
2: begin
mipi_rgb = mipi_line_toggle ? mipi_data_out2[15:8] : mipi_data_out1[15:8];
mipi_wr = 0;
mipi_cs = 0;
mipi_dc = 1;
mipi_rd = 1;
mipi_x = mipi_x + 1'b1;
if(mipi_x_toggle != 2'd3 || ~switches[2]) begin
mipi_index = mipi_index + 1'b1;
end else begin
mipi_x_toggle = 0;
end
mipi_x_toggle = mipi_x_toggle + 1'b1;
end
3: begin
mipi_wr = 1;
mipi_cs = 0;
mipi_dc = 1;
mipi_rd = 1;
end
default: begin end
endcase
mipi_wr_state = mipi_wr_state + 1'b1;
if(mipi_x > 240) begin
mipi_x = 0;
mipi_index = 2;
mipi_x_toggle = 0;
mipi_wr_state = 0;
if(((mipi_y_toggle != 3'd3 && mipi_y_toggle != 3'd5) | ~switches[2])) begin
mipi_state = 3'd4;
end else begin
mipi_state = 3'd2;
mipi_y_toggle = mipi_y_toggle == 3'd5 ? 3'd0 : mipi_y_toggle;
end
mipi_y_toggle = mipi_y_toggle + 1'b1;
end
end
4: begin
mipi_state = render_mode == 2'd0 ? 3'd4 : 3'd1;
end
default:
begin
mipi_state = 2'b01;
end
endcase
if(~Reg_LCDcontrol_ff40[7]) begin
mipi_state = mipi_state ? 3'd1 : 3'd0;
end
end
end
//-------------------------------------------
//
// snes controller
//
//-------------------------------------------
reg [12:0] snes_counter;
always @(posedge cpu_clock)
begin
if(rst) begin
snes_state = 0;
snes_latch = 0;
snes_ctr = 0;
snes_buttons = 0;
snes_shift_reg = 0;
end else begin
snes_clk = ~(~|snes_counter[3:0] & snes_state[1:0]==2);
if(~|snes_counter[3:0]) begin
case (snes_state)
0: begin
snes_latch = 0;
snes_state = ~|snes_counter ? 2'b1 : 2'b0;
end
1: begin
snes_latch = 1;
snes_state = 2;
snes_shift_reg = 0;
snes_ctr = 0;
end
2: begin
snes_shift_reg = {snes_data, snes_shift_reg[15:1]};
snes_latch = 0;
snes_ctr = snes_ctr + 1'b1;
if(snes_ctr == 16) begin
snes_buttons = snes_shift_reg;
snes_state = 0;
end
end
default: snes_state = 0;
endcase
end
snes_counter = snes_counter + 1'b1;
led = snes_buttons[8:5] | { snes_buttons[4:2], snes_buttons[0]};
//led = snes_buttons[12:9] ^ {3'b0, ^fb_data_out};
end
Reg_buttons_ff00[0] = ~Reg_buttons_ff00[5] & snes_buttons[8] | ~Reg_buttons_ff00[4] & snes_buttons[7];
Reg_buttons_ff00[1] = ~Reg_buttons_ff00[5] & snes_buttons[0] | ~Reg_buttons_ff00[4] & snes_buttons[6];
Reg_buttons_ff00[2] = ~Reg_buttons_ff00[5] & snes_buttons[2] | ~Reg_buttons_ff00[4] & snes_buttons[4];
Reg_buttons_ff00[3] = ~Reg_buttons_ff00[5] & snes_buttons[3] | ~Reg_buttons_ff00[4] & snes_buttons[5];
end
//-------------------------------------------
//
// sound hardware
//
//-------------------------------------------
gb_sound_hardware gbsh(
.clk (pixel_clock), //the pixel clock
.rst (rst),
.left_out (sound_left_out),
.right_out (sound_right_out),
.sound_enabled (sound_enabled),
.addr_bus (cpu_addr_bus) ,
.data_bus_in (cpu_data_bus_out),
.data_bus_out (sound_data_bus_out),
.we (cpu_we),
.re (cpu_re),
.channel_enables (switches)
);
wire sound_enabled;
wire [15:0] sound_left_out;
wire [15:0] sound_right_out;
wire [7:0] sound_data_bus_out;
reg [7:0] sound_44khz_divider;
reg [5:0] sound_left_count;
reg [5:0] sound_right_count;
reg [15:0] sound_shifter;
wire sound_44khz_clk;
always @(posedge dac_mclk)
begin
sound_44khz_divider = sound_44khz_divider + 1'b1;
end
always_comb
begin
sound_44khz_clk = sound_44khz_divider[7];
dac_LR_clk <= sound_44khz_clk;
end
always @(posedge dac_bclk)
begin
if (rst) begin
sound_left_count = 6'b0;
sound_right_count = 6'b0;
end else if (~i2c_init) begin
//do something here?
end else if(sound_44khz_clk) begin
sound_right_count = 6'b0;
if(~|sound_left_count) begin
sound_shifter = sound_left_out;
end
if(~sound_left_count[5]) begin
sound_shifter = {sound_shifter[14:0], 1'b0};
sound_left_count = sound_left_count + 1'b1;
end
end else begin
sound_left_count = 6'b0;
if(~|sound_right_count) begin
sound_shifter = sound_right_out;
end
if(~sound_right_count[5]) begin
sound_shifter = {sound_shifter[14:0], 1'b0};
sound_right_count = sound_right_count + 1'b1;
end
end
dac_data = sound_shifter[15];
end
//-------------------------------------------
//
// I2C for audio DAC
//
//-------------------------------------------
reg start_i2c;
reg done_i2c;
wire ack_i2c;
wire [15:0] lut_data;
reg [3:0] lut_index = 0;
reg [7:0] dac_i2c_addr = 8'h34;
reg sending_i2c;
wire i2c_init;
i2c_send i2c_dac(
.clk (pixel_clock), //the clock, max 526kHz * 8 for the dac
.rst (rst),
.sclk (i2c_sclk), //i2c sclk
.sdat (i2c_sdata), //i2c sdat
.start (start_i2c) , //starts the send/recieve
.done (done_i2c), //set to high when transfer complete
.ack (ack_i2c), //will be high if all three acks are correct
.data ({dac_i2c_addr, lut_data}) //data to be sent
);
always_comb begin
dac_mute <= 1'b1;
i2c_init <= lut_index == 4'hb ? 1'b1 : 1'b0;
case (lut_index)
4'h0: lut_data <= 16'h0c13; // power on everything except out
4'h1: lut_data <= 16'h0017; // left input
4'h2: lut_data <= 16'h0217; // right input
4'h3: lut_data <= 16'h045c; // left output
4'h4: lut_data <= 16'h065c; // right output
4'h5: lut_data <= 16'h08d4; // analog path
4'h6: lut_data <= 16'h0a04; // digital path
4'h7: lut_data <= 16'h0e01; // digital IF
4'h8: lut_data <= 16'h1020; // sampling rate
4'h9: lut_data <= 16'h0c03; // power on everything
4'ha: lut_data <= 16'h1201; // activate
default: lut_data <= 16'h0000;
endcase
end
always @(posedge pixel_clock)
begin
if(rst) begin
lut_index = 1'b0;
sending_i2c = 1'b0;
start_i2c = 1'b0;
dac_i2c_addr = 8'h34;
end else if (lut_index != 4'hb) begin
if(~sending_i2c) begin
sending_i2c = 1'b1;
start_i2c = 1'b1;
end else if (done_i2c) begin
lut_index = lut_index + 1'b1;
start_i2c = 1'b0;
sending_i2c = 1'b0;
end
end
end
//-------------------------------------------
//
// CPU
//
//-------------------------------------------
wire [15:0] cpu_addr_bus;
wire [7:0] cpu_data_bus_out;
wire [7:0] cpu_data_bus_in;
wire cpu_we;
wire cpu_re;
wire [15:0] cpu_pc;
reg [7:0] irq;
wire button_pressed = 0;
wire cgb = 0;
wire initialized;
wire speed_double;
gb_cpu cpu(
.rst(rst),
.clock(cpu_clock),
.addr_bus_out(cpu_addr_bus),
.data_bus_in(cpu_data_bus_in),
.data_bus_out(cpu_data_bus_out),
.we(cpu_we),
.re(cpu_re),
.PC(cpu_pc),
.irq(irq),
.button_pressed(button_pressed),
.cgb(cgb),
.initialized(initialized),
.gdma_happening(gdma_happening),
.speed_double(speed_double));
//-------------------------------------------
//
// Memory controller
//
//-------------------------------------------
reg [7:0] mem_controller_data_out;
reg [7:0] dma_wr_addr;
reg [7:0] dma_data;
reg dma_we;
reg dma_happening;
wire [15:0] gdma_wr_addr;
wire [7:0] gdma_data;
wire gdma_we;
wire gdma_happening;
reg unloaded;
reg gb_rom;
gb_memory_controller mem_control(
.rst(rst),
.clock(memory_clock),
.addr_bus(cpu_addr_bus),
.data_in(cpu_data_bus_out),
.data_out(mem_controller_data_out),
.we(cpu_we),
.rd(cpu_re),
.cgb(cgb),
.initialized(initialized),
.dma_wr_addr(dma_wr_addr),
.dma_we(dma_we),
.dma_happening(dma_happening),
.dma_data(dma_data),
.gdma_wr_addr(gdma_wr_addr),
.gdma_we(gdma_we),
.gdma_happening(gdma_happening),
.gdma_data(gdma_data),
.unloaded(unloaded),
.gb_rom(gb_rom),
.uart_addr(uart_load_addr),
.uart_data_in(uart_data_rcv),
.uart_we(uart_load_we),
.uart_load(uart_loading));
//-------------------------------------------
//
// Video hardware
//
//-------------------------------------------
reg [15:0] vram_search_addr;
reg [15:0] char_addr;
reg [7:0] tile_attr;
reg [7:0] tile_attr_cur;
reg [7:0] data;
reg [7:0] q;
reg [1:0] bg_color_index;
reg [1:0] sprite_color_index;
reg [1:0] color_index;
reg [15:0] color;
reg [8:0] pixel_x;
reg [8:0] pixel_y;
//-------------------------------------------
//
// GB Registers
//
//-------------------------------------------
reg [7:0] Reg_buttons_ff00=0;
reg [7:0] Reg_SB_ff01=8'h00;
reg [7:0] Reg_SC_ff02=8'h00;
reg [7:0] Reg_palette_ff47=8'hC9; //zelda title screen palette
reg [7:0] Reg_palette_ff48=8'hC9; //zelda title screen palette
reg [7:0] Reg_palette_ff49=8'hC9; //zelda title screen palette
reg [7:0] Reg_lyc_ff45=8'b0;
reg [7:0] Reg_xscroll_ff43=0;
reg [7:0] Reg_yscroll_ff42=0;
reg [7:0] Reg_LCDcontrol_ff40=8'h00;
reg [7:0] Reg_LCDstatus_ff41=8'h00;
reg [7:0] Reg_winX_ff4b=8'h00;
reg [7:0] Reg_winY_ff4a=8'h00;
reg [7:0] Reg_vbank_ff4f = 0;
reg [7:0] Reg_TIMA_ff05 =8'h00;
reg [7:0] Reg_TMA_ff06 =8'h00;
reg [7:0] Reg_TAC_ff07 =8'h00;
//reg [7:0] Reg_IF_ff0f =8'h00;
//------------------------------------------
//
// Timer / Div
//
//------------------------------------------
reg [15:0] timer_accumulator;
wire timer_tick;
reg timer_tick_flag;
reg timer_tick_flag_last;
//-------------------------------------------
//
// Timer
//
//-------------------------------------------
always @(negedge timer_tick)
begin
if(Reg_TAC_ff07[2])
begin
timer_tick_flag = ~timer_tick_flag;
end
end
always @(posedge cpu_clock)
begin
if(rst) begin
Reg_TIMA_ff05 = 8'b0;
Reg_TMA_ff06 = 8'b0;
Reg_TAC_ff07 = 8'hF8;
timer_accumulator = 16'b0;
irq[2] = 0;
end else begin
casex (cpu_addr_bus)
16'b111111110xxxxxxx: begin
if(cpu_we) begin
case (cpu_addr_bus[6:0])
7'h04: timer_accumulator = 0;
7'h05: Reg_TIMA_ff05 = cpu_data_bus_out;
7'h06: Reg_TMA_ff06 = cpu_data_bus_out;
7'h07: Reg_TAC_ff07[2:0] = cpu_data_bus_out[2:0];
default: begin end
endcase
end
end
default: begin
end
endcase
if(timer_tick_flag_last ^ timer_tick_flag)
begin
timer_tick_flag_last = timer_tick_flag;
Reg_TIMA_ff05 = Reg_TIMA_ff05 + 1'b1;
irq[2] = ~|Reg_TIMA_ff05 ;
if(~|Reg_TIMA_ff05)
begin
Reg_TIMA_ff05 = Reg_TMA_ff06;
end
end else begin
irq[2] = 1'b0;
end
timer_accumulator = timer_accumulator + 1'b1;
end
end
//-------------------------------------------
//
// Pixel offsets
//
//-------------------------------------------
reg [8:0] x_offset;
reg [8:0] y_offset;
//-------------------------------------------
vga_controller_1280x1024 vga (clock_108, vs_, hs_, _vblank, _hblank, x, y);
vga_clock clocks(clock_50mhz, reset, clock_108, full_clock, dac_mclk, dac_bclk, uart_clk, clock_20mhz, locked);
//-------------------------------------------
//-------------------------------------------
//
// Video memory
//
//-------------------------------------------
reg vram_we;
reg [7:0] vram_data_in;
reg [7:0] vram1_data_out;
reg [15:0] vram1_addr_bus;
reg [15:0] vram1_search_addr;
reg [7:0] vram2_data_out;
reg [15:0] vram2_addr_bus;
reg [15:0] vram2_search_addr;
reg [7:0] vram_data_out;
gb_vram vram1(gdma_happening ? gdma_wr_addr : vram1_addr_bus[12:0],
memory_clock,
gdma_happening ? gdma_data :vram_data_in,
(vram_we | (gdma_happening & gdma_we)) & ~Reg_vbank_ff4f[0],
vram1_data_out);
//gb_vram vram2(gdma_happening ? gdma_wr_addr : vram2_addr_bus[12:0],
// memory_clock,
// gdma_happening ? gdma_data :vram_data_in,
// (vram_we | (gdma_happening & gdma_we)) & Reg_vbank_ff4f[0],
// vram2_data_out);
//-------------------------------------------
//
// OAM Mem
//
//-------------------------------------------
reg oam_we;
reg [7:0] oam_data_in;
reg [7:0] oam_data_out;
reg [7:0] oam_addr_bus;
reg [7:0] oam_wr_addr_bus;
gb_oam2 oam(
memory_clock,
oam_data_in,
oam_addr_bus[7:0], oam_wr_addr_bus,
oam_we | dma_we,
oam_data_out);
//-------------------------------------------
//
// Pixel clock state machine
//
//-------------------------------------------
parameter [2:0] PIXEL_STATE_Ba=3'd0,PIXEL_STATE_0a=3'd2,PIXEL_STATE_1a=3'd4,PIXEL_STATE_Sa=3'd6,
PIXEL_STATE_Bb=3'd1,PIXEL_STATE_0b=3'd3,PIXEL_STATE_1b=3'd5,PIXEL_STATE_Sb=3'd7;
reg [2:0] pixel_state;
reg [7:0] in_x;
wire valid_vram_addr;
always_comb
begin
vram_data_in <= cpu_data_bus_out;
oam_wr_addr_bus <= dma_happening ? dma_wr_addr : cpu_addr_bus[7:0];
case (render_mode)
0: begin
vram1_addr_bus <= cpu_addr_bus;
vram2_addr_bus <= cpu_addr_bus;
oam_addr_bus <= cpu_addr_bus[7:0];
end
1: begin
vram1_addr_bus <= cpu_addr_bus;
vram2_addr_bus <= cpu_addr_bus;
oam_addr_bus <= cpu_addr_bus[7:0];
end
2: begin
vram1_addr_bus <= cpu_addr_bus;
vram2_addr_bus <= cpu_addr_bus;
oam_addr_bus <= Reg_LCDcontrol_ff40[7] ? oam_search_addr :cpu_addr_bus[7:0];
end
3: begin
vram1_addr_bus <= Reg_LCDcontrol_ff40[7] ? vram1_search_addr : cpu_addr_bus;
vram2_addr_bus <= Reg_LCDcontrol_ff40[7] ? vram2_search_addr : cpu_addr_bus;
oam_addr_bus <= Reg_LCDcontrol_ff40[7] ? oam_search_addr : cpu_addr_bus[7:0];
end
endcase
vram_data_out <= Reg_vbank_ff4f[0] & gbc ? vram2_data_out : vram1_data_out;
oam_data_in <= dma_happening ? dma_data : cpu_data_bus_out;
rst <= ~rst_button ;
gbc = ~switches[0];
case (Reg_TAC_ff07[1:0])
2'b00: timer_tick <= Reg_TAC_ff07[2] & timer_accumulator[9];
2'b01: timer_tick <= Reg_TAC_ff07[2] & timer_accumulator[3];
2'b10: timer_tick <= Reg_TAC_ff07[2] & timer_accumulator[5];
2'b11: timer_tick <= Reg_TAC_ff07[2] & timer_accumulator[7];
endcase
casex (cpu_addr_bus)
16'h8xxx, 16'h9xxx: begin
cpu_data_bus_in = render_mode == 2'b11 ? 8'hFF : vram_data_out;
vram_we = render_mode == 2'b11 ? 1'b0 : cpu_we;
oam_we = 1'b0;
end
16'b111111110xxxxxxx: begin
vram_we <= 1'b0;
oam_we <= 1'b0;
casex (cpu_addr_bus[6:0])
7'h01: cpu_data_bus_in = serial_xfr_complete ? 8'hff : Reg_SB_ff01 ;
7'h02: cpu_data_bus_in = Reg_SC_ff02 ;
7'h04: cpu_data_bus_in = timer_accumulator[15:8];
7'h05: cpu_data_bus_in = Reg_TIMA_ff05;
7'h06: cpu_data_bus_in = Reg_TMA_ff06;
7'h07: cpu_data_bus_in = Reg_TAC_ff07;
7'h08: cpu_data_bus_in = {7'b0,uart_data_rdy};
7'h09: cpu_data_bus_in = uart_data_rcv;
7'h45: cpu_data_bus_in = Reg_lyc_ff45;
7'h44: cpu_data_bus_in = pixel_y[7:0];
7'h40: cpu_data_bus_in = Reg_LCDcontrol_ff40;
7'h41: cpu_data_bus_in = {Reg_LCDstatus_ff41[7:3],Reg_LCDcontrol_ff40[7] ? pixel_y == Reg_lyc_ff45 : 1'b1, Reg_LCDcontrol_ff40[7] ? render_mode : 2'b00};
7'h43: cpu_data_bus_in = Reg_xscroll_ff43;
7'h42: cpu_data_bus_in = Reg_yscroll_ff42;
7'h47: cpu_data_bus_in = Reg_palette_ff47;
7'h48: cpu_data_bus_in = Reg_palette_ff48;
7'h49: cpu_data_bus_in = Reg_palette_ff49;
7'h4b: cpu_data_bus_in = Reg_winX_ff4b;
7'h4a: cpu_data_bus_in = Reg_winY_ff4a;
7'h00: cpu_data_bus_in = Reg_buttons_ff00;