Skip to content

Commit

Permalink
Small hid and mfp adjustments finally fixing #61
Browse files Browse the repository at this point in the history
  • Loading branch information
harbaum committed Sep 6, 2024
1 parent 4027bbf commit 61a636d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
10 changes: 5 additions & 5 deletions src/atarist/atarist.v
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ end
reg [31:0] clk_cnt_mfp;
reg clk_mfp_en;

wire [31:0] SYSTEM_CLOCK = 32'd32084988;
wire [31:0] MFP_CLOCK = 32'd2457600;
localparam SYSTEM_CLOCK = 32'd32_084_988;
localparam MFP_CLOCK = 32'd2_457_600;

always @(posedge clk_32) begin
if(!porb) begin
Expand All @@ -142,10 +142,10 @@ always @(posedge clk_32) begin
end else begin
clk_mfp_en <= 1'b0;

if(clk_cnt_mfp < SYSTEM_CLOCK/2)
if(clk_cnt_mfp < SYSTEM_CLOCK)
clk_cnt_mfp <= clk_cnt_mfp + MFP_CLOCK;
else begin
clk_cnt_mfp <= clk_cnt_mfp - (SYSTEM_CLOCK/2 - MFP_CLOCK);
clk_cnt_mfp <= clk_cnt_mfp - SYSTEM_CLOCK + MFP_CLOCK;
clk_mfp_en <= 1'b1;
end
end
Expand Down Expand Up @@ -533,7 +533,7 @@ mfp mfp (
/* ------------------------------------------------------------------------------ */

wire ikbd_tx, ikbd_rx;

ikbd #( .DIV(4) ) ikbd (
.clk(clk_32), // clock is divided to 2MHz (/16 == 2^4) inside ikbd
.res(peripheral_reset),
Expand Down
65 changes: 34 additions & 31 deletions src/misc/mcu_spi.v
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ module mcu_spi (
output reg spi_io_dout,

// byte interface to the various core components
output mcu_sys_strobe, // byte strobe for system control target
output mcu_hid_strobe, // byte strobe for HID target
output mcu_osd_strobe, // byte strobe for OSD target
output mcu_sdc_strobe, // byte strobe for SD card target
output reg mcu_sys_strobe, // byte strobe for system control target
output reg mcu_hid_strobe, // byte strobe for HID target
output reg mcu_osd_strobe, // byte strobe for OSD target
output reg mcu_sdc_strobe, // byte strobe for SD card target
output mcu_start,
input [7:0] mcu_sys_din,
input [7:0] mcu_hid_din,
Expand Down Expand Up @@ -54,56 +54,59 @@ always @(negedge spi_io_clk or posedge spi_io_ss) begin
if(spi_cnt[2:0] == 3'd7) begin
// latch byte and raise ready flag
spi_data_in <= { spi_sr_in, spi_io_din };
spi_data_in_ready <= 1'b1;
spi_data_in_ready <= !spi_data_in_ready;
end

// Clear data ready at bit 3. If the reading side
// runs at 32 Mhz, this will work up to 64 MHz
// SPI clock. At higher clocks the word width must
// be increased.
if(spi_cnt[2:0] == 3'd3)
spi_data_in_ready <= 1'b0;
// if(spi_cnt[2:0] == 3'd3)
// spi_data_in_ready <= 1'b0;
end
end // always @ (negedge spi_io_clk or posedge spi_io_ss)

// bring received byte into local clock domain


reg spi_in_strobe;
reg [7:0] spi_target;
assign mcu_sys_strobe = spi_in_strobe && spi_target == 8'd0;
assign mcu_hid_strobe = spi_in_strobe && spi_target == 8'd1;
assign mcu_osd_strobe = spi_in_strobe && spi_target == 8'd2;
assign mcu_sdc_strobe = spi_in_strobe && spi_target == 8'd3;

reg [7:0] spi_in_data;
assign mcu_start = spi_in_cnt == 2;
assign mcu_dout = spi_in_data;

reg [7:0] spi_target;
reg [3:0] spi_in_cnt;

always @(posedge clk) begin
reg [1:0] spi_data_in_readyD;

spi_data_in_readyD <= { spi_data_in_readyD[0], spi_data_in_ready };

mcu_sys_strobe <= 1'b0;
mcu_hid_strobe <= 1'b0;
mcu_osd_strobe <= 1'b0;
mcu_sdc_strobe <= 1'b0;

if(spi_io_ss)
spi_in_cnt <= 4'd0;

if(spi_data_in_readyD == 2'b01) begin
// incoming SPI byte is now in local clock domain
else begin
// parse incoming bit whenever ready toggles
// if(spi_data_in_readyD == 2'b01) begin
if(spi_data_in_readyD[1] ^ spi_data_in_readyD[0]) begin
// incoming SPI byte is now in local clock domain

// first byte is target id. Else send external trigger
if(spi_in_cnt == 4'd0)
spi_target <= spi_data_in;
else begin
spi_in_strobe <= 1'b1;
spi_in_data <= spi_data_in;
end
// first byte is target id. Else send external trigger
if(spi_in_cnt == 4'd0)
spi_target <= spi_data_in;
else begin
if(spi_target == 8'd0) mcu_sys_strobe <= 1'b1;
if(spi_target == 8'd1) mcu_hid_strobe <= 1'b1;
if(spi_target == 8'd2) mcu_osd_strobe <= 1'b1;
if(spi_target == 8'd3) mcu_sdc_strobe <= 1'b1;

spi_in_data <= spi_data_in;
end

if(spi_in_cnt != 4'd15)
spi_in_cnt <= spi_in_cnt + 4'd1;
end else
spi_in_strobe <= 1'b0;
if(spi_in_cnt != 4'd15)
spi_in_cnt <= spi_in_cnt + 4'd1;
end
end
end

wire [7:0] in_byte =
Expand Down

0 comments on commit 61a636d

Please sign in to comment.