2 // Copyright (C) 2008 Tomasz Malesinski <tmal@mimuw.edu.pl>
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 module antic_ms_hcount_seq(clk_i
, ms_hcount
,
19 new_block
, dma_block
, char_block
, dma_pf_width
, ir
,
20 shift_reg_shift
, load_pf
, load_char
, load_out
,
22 input clk_i
, ms_hcount
;
23 input new_block
, dma_block
, char_block
, dma_pf_width
, ir
;
24 output shift_reg_shift
, load_pf
, load_char
, load_out
, out_reg_shift
;
28 wire new_block
, dma_block
, char_block
;
29 wire [1:0] dma_pf_width
;
32 reg load_pf
, load_char
;
36 reg load_pf_0
, load_char_0
;
37 reg [3:0] pf_byte_mod
;
38 reg [1:0] pf_pixel_mod
;
106 assign shift_reg_shift
= (ms_hcount
>= 3) && (ms_hcount
< 192 + 3) &&
107 (ms_hcount
[1:0] == 3);
109 assign out_reg_shift
= ((ms_hcount
[1:0] & pf_pixel_mod
) ==
110 (2'd2 & pf_pixel_mod
));
112 always @ (new_block
or dma_block
or dma_pf_width
or ms_hcount
or
113 pf_byte_mod
or ir
) begin
114 pf_cyc
= char_block ?
3 : 7;
116 if (new_block
&& dma_block
) begin
117 if (dma_pf_width
== 1 && !ir
[4])
118 load_pf_0
= ((ms_hcount
& pf_byte_mod
) == (pf_cyc
& pf_byte_mod
)) &&
119 (ms_hcount
>= 32 + pf_cyc
) &&
120 (ms_hcount
< 160 + pf_cyc
);
121 else if (dma_pf_width
== (ir
[4] ?
1 : 2))
122 load_pf_0
= ((ms_hcount
& pf_byte_mod
) == (pf_cyc
& pf_byte_mod
)) &&
123 (ms_hcount
>= 16 + pf_cyc
) &&
124 (ms_hcount
< 176 + pf_cyc
);
125 else if (dma_pf_width
== 3 ||
(ir
[4] && dma_pf_width
== 2))
126 load_pf_0
= ((ms_hcount
& pf_byte_mod
) == (pf_cyc
& pf_byte_mod
)) &&
127 (ms_hcount
>= pf_cyc
) && (ms_hcount
< 192 + pf_cyc
);
131 always @ (char_block
or dma_block
or dma_pf_width
or ms_hcount
or
134 if (char_block
&& dma_block
) begin
135 if (dma_pf_width
== 1 && !ir
[4])
136 load_char_0
= ((ms_hcount
& pf_byte_mod
) == (9 & pf_byte_mod
)) &&
137 (ms_hcount
>= 32 + 9 && ms_hcount
< 160 + 9);
138 else if (dma_pf_width
== (ir
[4] ?
1 : 2))
139 load_char_0
= ((ms_hcount
& pf_byte_mod
) == (9 & pf_byte_mod
)) &&
140 (ms_hcount
>= 16 + 9 && ms_hcount
< 176 + 9);
141 else if (dma_pf_width
== 3 ||
(ir
[4] && dma_pf_width
== 2))
142 load_char_0
= ((ms_hcount
& pf_byte_mod
) == (9 & pf_byte_mod
)) &&
143 (ms_hcount
>= 9 && ms_hcount
< 192 + 9);
147 assign load_out
= ((ms_hcount
[3:0] & pf_byte_mod
) == (4'd14 & pf_byte_mod
));
149 always @ (posedge clk_i
) begin
150 load_pf
<= load_pf_0
;
151 load_char
<= load_char_0
;
155 module antic_shift_reg(clk_i
, shift
, load
, in
, out
);
169 reg [7:0] shift_reg
[0:47];
173 assign out
= shift_reg
[1];
175 always @ (posedge clk_i
) begin
177 for (i
= 0; i
< 47; i
= i
+ 1)
178 shift_reg
[i
+ 1] <= shift_reg
[i
];
182 shift_reg
[0] <= shift_reg
[47];
186 module antic(rst_i
, clk_i
,
188 slavedat_i
, masterdat_i
,
217 wire [7:0] slavedat_i
;
218 wire [7:0] masterdat_i
;
230 reg [2:0] pf_antic_out
, pf_antic_out_d
;
233 reg [1:0] dma_pf_width
;
245 wire nmireq_dli
, nmireq_vbi
;
246 reg nmist_dli
, nmist_vbi
;
247 reg nmien_dli
, nmien_vbi
;
249 reg [15:0] dlist_ctr
;
250 reg [7:0] dlist_ctr_tmp
;
253 reg [15:0] memscan_ctr
;
258 wire [3:0] chr_dcount
;
284 wire [1:0] dma_ply_num
;
286 wire load_out_p
, load_out
;
288 wire hblank
, vblank
, vsync
;
291 wire shift_reg_shift
;
292 wire [7:0] shift_reg_out
;
294 reg [7:0] char_data
, out_reg
;
296 reg [1:0] char_color_p
, char_color
;
298 assign ack_o
= stb_i
;
301 always @ (adr_i
or vcount
or nmist_dli
or nmist_vbi
)
306 dat_o
= {nmist_dli
, nmist_vbi
, 6'b0};
312 always @ (posedge clk_i
)
313 if (stb_i
&& we_i
&& adr_i
== 'h0
) begin
314 dma_pf_width
<= slavedat_i
[1:0];
315 dma_mis_en
<= slavedat_i
[2];
316 dma_ply_en
<= slavedat_i
[3];
317 dma_pm_1res
<= slavedat_i
[4];
318 dma_instr_en
<= slavedat_i
[5];
322 always @ (posedge clk_i
)
323 if (stb_i
&& we_i
&& adr_i
== 'h1
)
324 chactl
<= slavedat_i
[2:0];
327 always @ (posedge clk_i
)
328 if (stb_i
&& we_i
&& adr_i
== 'h2
)
329 dlist_ctr
[7:0] <= slavedat_i
;
330 else if (stb_i
&& we_i
&& adr_i
== 'h3
)
331 dlist_ctr
[15:8] <= slavedat_i
;
332 else if (dl_load
) begin
334 dlist_ctr
[9:0] <= dlist_ctr
[9:0] + 1;
336 dlist_ctr
[15:8] <= masterdat_i
;
337 dlist_ctr
[7:0] <= dlist_ctr_tmp
;
340 dlist_ctr_tmp
<= masterdat_i
;
344 always @ (posedge clk_i
)
345 if (stb_i
&& we_i
&& adr_i
== 'h4
)
346 hscrol
<= slavedat_i
[3:0];
349 always @ (posedge clk_i
)
350 if (stb_i
&& we_i
&& adr_i
== 'h5
)
351 vscrol
<= slavedat_i
[3:0];
354 always @ (posedge clk_i
)
355 if (stb_i
&& we_i
&& adr_i
== 'h7
)
356 pmbase
<= slavedat_i
[7:2];
359 always @ (posedge clk_i
)
360 if (stb_i
&& we_i
&& adr_i
== 'h9
)
361 chbase
<= slavedat_i
[7:1];
364 always @ (posedge clk_i
)
365 if (rst_i || hcount
== 206)
367 else if (stb_i
&& we_i
&& adr_i
== 'ha
)
371 always @ (posedge clk_i
)
375 end else if (stb_i
&& we_i
&& adr_i
== 'he
) begin
376 nmien_vbi
<= slavedat_i
[6];
377 nmien_dli
<= slavedat_i
[7];
381 always @ (posedge clk2_i
)
384 else if (hcount
== 227)
387 hcount
<= hcount
+ 1;
390 always @ (posedge clk2_i
)
393 else if (hcount
== 227)
397 vcount
<= vcount
+ 1;
399 // Display list interrupt.
400 assign nmireq_dli
= (hcount
== 16 && dcount
== maxline
&& dli
&&
401 nmien_dli
&& !vblank
&& !wait_vblank
);
403 // Vertical blank interrupt.
404 assign nmireq_vbi
= (hcount
== 16 && vcount
== 240 && nmien_vbi
);
406 always @ (posedge clk_i
)
410 end else if (nmireq_vbi
) begin
413 end else if (nmireq_dli
) begin
416 end else if (stb_i
&& we_i
&& adr_i
=='hf
) begin
421 assign nmi
= nmireq_dli | nmireq_vbi
;
423 always @ (posedge clk2_i
)
427 (dcount
== ((last_vscrol_en
&& !vscrol_en
) ? vscrol
: maxline
) &&
428 !wait_vblank
&& !vblank
&& dma_instr_en
)))
433 assign load_instr
= new_block
&& (hcount
== 2);
435 assign vscrol_en
= dma_block
&& ir
[5];
437 always @ (posedge clk2_i
)
438 if (hcount
== 0 && new_block
)
439 last_vscrol_en
<= vscrol_en
;
442 always @ (posedge clk2_i
)
445 else if (hcount
== 6)
447 dcount
<= (!last_vscrol_en
&& vscrol_en
) ? vscrol
: 0;
449 dcount
<= dcount
+ 1;
451 // Memory Scan Counter.
452 always @ (posedge clk_i
)
454 memscan_ctr
[11:0] <= memscan_ctr
[11:0] + 1;
455 else if (load_memscanl
)
456 memscan_ctr
[7:0] <= masterdat_i
;
457 else if (load_memscanh
)
458 memscan_ctr
[15:8] <= masterdat_i
;
460 // Instruction register.
461 always @ (posedge clk_i
)
464 else if (vcount
== 0)
467 // Instruction decoder.
473 'h0
: maxline
= ir
[6:4];
549 assign wait_vblank
= (ir
== 'h41
);
550 assign dma_block
= (ir
[3:0] != 0 && ir
[3:0] != 1);
552 assign load_dlptrl
= new_block
&& (ir
[3:0] == 1) && (hcount
== 12);
553 assign load_dlptrh
= new_block
&& (ir
[3:0] == 1) && (hcount
== 14);
555 assign load_memscanl
= new_block
&& dma_block
&& ir
[6] && (hcount
== 12);
556 assign load_memscanh
= new_block
&& dma_block
&& ir
[6] && (hcount
== 14);
558 assign load_mis
= !vblank
&& dma_mis_en
&& (hcount
== 0);
559 assign load_ply
= !vblank
&& dma_ply_en
&&
560 (hcount
== 4 || hcount
== 6 ||
561 hcount
== 8 || hcount
== 10);
562 assign dma_ply_num
= (hcount
>> 1) - 2;
564 assign dl_load
= load_instr || load_memscanh || load_memscanl ||
565 load_dlptrh || load_dlptrl
;
567 always @ (posedge clk2_i
)
568 if (hcount
== 16 + (ir
[4] ?
(hscrol
& ~1) : 0))
571 ms_hcount
<= ms_hcount
+ 1;
573 antic_ms_hcount_seq
u_ms_hcount_seq(.
clk_i(clk_i
),
574 .
ms_hcount(ms_hcount
),
575 .
new_block(new_block
),
576 .
dma_block(dma_block
),
577 .
char_block(char_block
),
578 .
dma_pf_width(dma_pf_width
),
580 .
shift_reg_shift(shift_reg_shift
),
582 .
load_char(load_char
),
584 .
out_reg_shift(out_reg_shift
));
586 always @ (hcount
or dma_pf_width
or dma_instr_en
or vblank
) begin
587 if (!dma_instr_en || vblank
)
592 1: dwin
= (hcount
>= 64 && hcount
< 192);
593 2: dwin
= (hcount
>= 48 && hcount
< 208);
594 3: dwin
= (hcount
>= 44 && hcount
< 220);
598 assign hblank
= (hcount
< 34 || hcount
>= 222);
599 assign vblank
= (vcount
< 8 || vcount
>= 240);
601 // TODO: lines here are approximate.
602 assign vsync
= (vcount
>= 300 && vcount
< 303);
604 assign char_blank
= (ir
[3:0] == 3) &&
605 (((chr_dcount
== 0 || chr_dcount
== 1) &&
606 shift_reg_out
[6:5] == 2'b11) ||
607 ((chr_dcount
== 8 || chr_dcount
== 9) &&
608 shift_reg_out
[6:5] != 2'b11));
610 always @ (posedge clk_i
)
612 // TODO: change name out_reg_p na char_data
613 char_data
<= char_blank ?
0 : masterdat_i
;
614 char_color_p
<= shift_reg_out
[7:6];
617 always @ (posedge clk2_i
)
619 out_reg
<= char_block ? char_data
: shift_reg_out
;
620 char_color
<= char_color_p
;
622 else if (out_reg_shift
)
624 out_reg
<= {out_reg
[6:0], 1'b0};
626 out_reg
<= {out_reg
[5:0], 2'b00};
628 always @ (ir
or out_reg
or char_color
or chactl
)
629 if (ir
[3:0] == 2 || ir
[3:0] == 3)
631 pf_antic_out
= {1'b1,
632 (out_reg
[7:6] & ~{2{chactl
[0]}}) ^
{2{chactl
[1]}}};
634 pf_antic_out
= {1'b1, out_reg
[7:6]};
635 else if (ir
[3:0] == 'hf
)
636 pf_antic_out
= {1'b1, out_reg
[7:6]};
637 else if (ir
[3:0] == 4 || ir
[3:0] == 5)
639 0: pf_antic_out
= 3'b000;
640 1: pf_antic_out
= 3'b100;
641 2: pf_antic_out
= 3'b101;
642 3: pf_antic_out
= char_color
[1] ?
3'b111 : 3'b110;
644 else if (ir
[3:0] == 6 || ir
[3:0] == 7)
646 pf_antic_out
= {1'b1, char_color
};
648 pf_antic_out
= 3'b000;
649 else if (ir
[3:0] == 8 || ir
[3:0] == 'ha || ir
[3:0] == 'hd ||
652 0: pf_antic_out
= 3'b000;
653 1: pf_antic_out
= 3'b100;
654 2: pf_antic_out
= 3'b101;
655 3: pf_antic_out
= 3'b110;
657 else if (ir
[3:0] == 9 || ir
[3:0] == 'hb || ir
[3:0] == 'hc
)
658 pf_antic_out
= out_reg
[7] ?
3'b100 : 3'b000;
660 pf_antic_out
= 3'b000;
662 always @ (posedge clk2_i
)
663 pf_antic_out_d
<= pf_antic_out
;
665 always @ (vsync
or vblank
or hblank
or dwin
or ir
or pf_antic_out
or
666 pf_antic_out_d
) begin
669 else if (hblank || vblank
)
670 if (ir
[3:0] == 2 || ir
[3:0] == 3 || ir
[3:0] == 'hf
)
675 antic_out
= (dma_block
&& ir
[4] && hscrol
[0]) ?
676 pf_antic_out_d
: pf_antic_out
;
681 assign chr_dcount
= dcount ^
{4{chactl
[2]}};
683 always @ (dl_load
or dlist_ctr
or load_mis
or load_ply
or
684 pmbase
or vcount
or dma_ply_num
or
685 load_pf
or memscan_ctr
or
686 load_char
or chbase
or shift_reg_out
or dcount
) begin
690 adr_o
= dma_pm_1res ?
691 {pmbase
[5:1], 3'b011, vcount
[7:0]} :
692 {pmbase
[5:0], 3'b011, vcount
[7:1]};
694 adr_o
= dma_pm_1res ?
695 {pmbase
[5:1], 1'b1, dma_ply_num
, vcount
[7:0]} :
696 {pmbase
[5:0], 1'b1, dma_ply_num
, vcount
[7:1]};
701 2: adr_o
= {chbase
[6:1], shift_reg_out
[6:0], chr_dcount
[2:0]};
702 3: adr_o
= {chbase
[6:1], shift_reg_out
[6:0], chr_dcount
[2:0]};
703 4: adr_o
= {chbase
[6:1], shift_reg_out
[6:0], chr_dcount
[2:0]};
704 5: adr_o
= {chbase
[6:1], shift_reg_out
[6:0], chr_dcount
[3:1]};
705 6: adr_o
= {chbase
[6:0], shift_reg_out
[5:0], chr_dcount
[2:0]};
706 7: adr_o
= {chbase
[6:0], shift_reg_out
[5:0], chr_dcount
[3:1]};
709 adr_o
= 0; // TODO: load some pointer by default
712 assign stb_o
= dl_load || load_mis || load_ply || load_pf || load_char ||
714 assign cyc_o
= stb_o
;
716 antic_shift_reg
u_shift_reg(.
clk_i(clk_i
),
717 .
shift(shift_reg_shift
),
720 .
out(shift_reg_out
));