Apple1_MiST/rtl/clock.v

53 lines
1.5 KiB
Coq
Raw Normal View History

2021-12-29 15:18:10 +00:00
module clock
(
2022-01-02 20:50:26 +00:00
input sys_clock, // master clock at cpu x 7 x 8
2022-01-02 15:24:57 +00:00
input reset, // reset
2022-01-09 16:38:47 +00:00
output pixel_clken, // 7MHz clock enable for the display
output cpu_clken, // 1MHz clock enable for the CPU, ram refresh cycles inlcuded
output cpu_clken_noRF, // 1MHz clock (pure, without refresh cycles)
output cpu_clock
2021-12-29 15:18:10 +00:00
);
2022-01-08 11:28:06 +00:00
localparam CPU_DIVISOR = 56; // (sys_clock / CPU_DIVISOR) = 1 MHz
localparam PIXEL_DIVISOR = 8; // (sys_clock / PIXEL_DIVISOR) = 7 MHz
2022-01-09 16:38:47 +00:00
localparam REFRESH_DIVISOR = 65; // counts 65 clock ticks (one complete scanline at phi0 speed)
2022-01-02 20:50:26 +00:00
reg [5:0] counter_cpu;
reg [2:0] counter_pixel;
2022-01-08 11:28:06 +00:00
reg [7:0] counter_refresh;
2022-01-02 20:50:26 +00:00
2022-01-02 15:24:57 +00:00
always @(posedge sys_clock or posedge reset)
begin
if(reset) begin
2022-01-08 11:28:06 +00:00
counter_cpu <= 0;
counter_pixel <= 0;
counter_refresh <= 0;
2022-01-02 15:24:57 +00:00
end
else begin
2022-01-02 20:50:26 +00:00
2022-01-08 11:28:06 +00:00
if (counter_cpu == (CPU_DIVISOR-1)) begin
counter_cpu <= 0;
counter_refresh <= (counter_refresh == REFRESH_DIVISOR-1) ? 0 : counter_refresh + 1;
end
else
counter_cpu <= counter_cpu + 1;
2022-01-02 20:50:26 +00:00
2022-01-08 11:28:06 +00:00
counter_pixel <= (counter_pixel == PIXEL_DIVISOR-1) ? 0 : counter_pixel + 1;
2022-01-02 15:24:57 +00:00
end
end
2022-01-02 21:20:44 +00:00
2022-01-09 16:38:47 +00:00
// the ram refresh cycle is activated by the horizontal counter on every 10 character
wire RF = counter_refresh == 25 || counter_refresh == 35 || counter_refresh == 45 || counter_refresh == 55;
assign cpu_clken = counter_cpu == 0 && !RF;
assign cpu_clken_noRF = counter_cpu == 0;
assign pixel_clken = counter_pixel == 0;
assign cpu_clock = counter_pixel < 4 ? 1 : 0;
2021-12-29 15:18:10 +00:00
endmodule