Skip to content

Commit

Permalink
Update MiST user_io/osd
Browse files Browse the repository at this point in the history
  • Loading branch information
gyurco committed Jul 2, 2019
1 parent 85af0e7 commit daf5153
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
8 changes: 4 additions & 4 deletions mist/MSX.sv
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ wire [31:0] img_size;
wire ps2_kbd_clk;
wire ps2_kbd_data;

wire [7:0] mouse_x;
wire [7:0] mouse_y;
wire [8:0] mouse_x;
wire [8:0] mouse_y;
wire [7:0] mouse_flags;
wire mouse_strobe;

Expand Down Expand Up @@ -237,8 +237,8 @@ always @(posedge clk_sys) begin
else if (~&joya) mouse_en <= 0;

if (mouse_strobe) begin
mouse_x_latch <= ~{mouse_flags[4], mouse_x} + 1'd1; //2nd complement of x
mouse_y_latch <= {mouse_flags[5], mouse_y};
mouse_x_latch <= ~mouse_x + 1'd1; //2nd complement of x
mouse_y_latch <= mouse_y;
end

mouse[5:4] <= ~mouse_flags[1:0];
Expand Down
42 changes: 29 additions & 13 deletions mist/osd.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ module osd (
input SPI_SS3,
input SPI_DI,

input [1:0] rotate, //[0] - rotate [1] - left or right

// VGA signals coming from core
input [5:0] R_in,
input [5:0] G_in,
input [5:0] B_in,
input HSync,
input VSync,

// VGA signals going to video connector
output [5:0] R_out,
output [5:0] G_out,
Expand Down Expand Up @@ -91,7 +93,7 @@ reg [9:0] vs_low, vs_high;
wire vs_pol = vs_high < vs_low;
wire [9:0] dsp_height = vs_pol ? vs_low : vs_high;

wire doublescan = (dsp_height>350);
wire doublescan = (dsp_height>350);

reg ce_pix;
always @(negedge clk_sys) begin
Expand Down Expand Up @@ -124,13 +126,13 @@ always @(posedge clk_sys) begin
hsD2 <= hsD;

// falling edge of HSync
if(!hsD && hsD2) begin
if(!hsD && hsD2) begin
h_cnt <= 0;
hs_high <= h_cnt;
end

// rising edge of HSync
else if(hsD && !hsD2) begin
else if(hsD && !hsD2) begin
h_cnt <= 0;
hs_low <= h_cnt;
v_cnt <= v_cnt + 1'd1;
Expand All @@ -142,13 +144,13 @@ always @(posedge clk_sys) begin
vsD2 <= vsD;

// falling edge of VSync
if(!vsD && vsD2) begin
if(!vsD && vsD2) begin
v_cnt <= 0;
vs_high <= v_cnt;
end

// rising edge of VSync
else if(vsD && !vsD2) begin
else if(vsD && !vsD2) begin
v_cnt <= 0;
vs_low <= v_cnt;
end
Expand All @@ -160,17 +162,31 @@ wire [9:0] h_osd_start = ((dsp_width - OSD_WIDTH)>> 1) + OSD_X_OFFSET;
wire [9:0] h_osd_end = h_osd_start + OSD_WIDTH;
wire [9:0] v_osd_start = ((dsp_height- (OSD_HEIGHT<<doublescan))>> 1) + OSD_Y_OFFSET;
wire [9:0] v_osd_end = v_osd_start + (OSD_HEIGHT<<doublescan);
wire [9:0] osd_hcnt = h_cnt - h_osd_start + 1'd1; // one pixel offset for osd_byte register
wire [9:0] osd_hcnt = h_cnt - h_osd_start;
wire [9:0] osd_vcnt = v_cnt - v_osd_start;
wire [9:0] osd_hcnt_next = osd_hcnt + 2'd1; // one pixel offset for osd pixel
wire [9:0] osd_hcnt_next2 = osd_hcnt + 2'd2; // two pixel offset for osd byte address register
reg osd_de;

wire osd_de = osd_enable &&
(HSync != hs_pol) && (h_cnt >= h_osd_start) && (h_cnt < h_osd_end) &&
(VSync != vs_pol) && (v_cnt >= v_osd_start) && (v_cnt < v_osd_end);
reg [10:0] osd_buffer_addr;
wire [7:0] osd_byte = osd_buffer[osd_buffer_addr];
reg osd_pixel;

reg [7:0] osd_byte;
always @(posedge clk_sys) if(ce_pix) osd_byte <= osd_buffer[{doublescan ? osd_vcnt[7:5] : osd_vcnt[6:4], osd_hcnt[7:0]}];
always @(posedge clk_sys) begin
if(ce_pix) begin
osd_buffer_addr <= rotate[0] ? {rotate[1] ? osd_hcnt_next2[7:5] : ~osd_hcnt_next2[7:5],
rotate[1] ? (doublescan ? ~osd_vcnt[7:0] : ~{osd_vcnt[6:0], 1'b0}) :
(doublescan ? osd_vcnt[7:0] : {osd_vcnt[6:0], 1'b0})} :
{doublescan ? osd_vcnt[7:5] : osd_vcnt[6:4], osd_hcnt_next2[7:0]};

wire osd_pixel = osd_byte[doublescan ? osd_vcnt[4:2] : osd_vcnt[3:1]];
osd_pixel <= rotate[0] ? osd_byte[rotate[1] ? osd_hcnt_next[4:2] : ~osd_hcnt_next[4:2]] :
osd_byte[doublescan ? osd_vcnt[4:2] : osd_vcnt[3:1]];

osd_de <= osd_enable &&
(HSync != hs_pol) && ((h_cnt + 1'd1) >= h_osd_start) && ((h_cnt + 1'd1) < h_osd_end) &&
(VSync != vs_pol) && (v_cnt >= v_osd_start) && (v_cnt < v_osd_end);
end
end

assign R_out = !osd_de ? R_in : {osd_pixel, osd_pixel, OSD_COLOR[2], R_in[5:3]};
assign G_out = !osd_de ? G_in : {osd_pixel, osd_pixel, OSD_COLOR[1], G_in[5:3]};
Expand Down
31 changes: 27 additions & 4 deletions mist/user_io.v
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,15 @@ module user_io #(parameter STRLEN=0, parameter PS2DIV=100) (
output ps2_mouse_clk,
output reg ps2_mouse_data,

// keyboard data
output reg key_pressed, // 1-make (pressed), 0-break (released)
output reg key_extended, // extended code
output reg [7:0] key_code, // key scan code
output reg key_strobe, // key data valid

// mouse data
output reg [7:0] mouse_x,
output reg [7:0] mouse_y,
output reg [8:0] mouse_x,
output reg [8:0] mouse_y,
output reg [7:0] mouse_flags, // YOvfl, XOvfl, dy8, dx8, 1, mbtn, rbtn, lbtn
output reg mouse_strobe, // mouse data is valid on mouse_strobe

Expand Down Expand Up @@ -383,12 +389,16 @@ always @(posedge clk_sys) begin
reg [7:0] mouse_flags_r;
reg [7:0] mouse_x_r;

reg key_pressed_r;
reg key_extended_r;

//synchronize between SPI and sys clock domains
spi_receiver_strobeD <= spi_receiver_strobe_r;
spi_receiver_strobe <= spi_receiver_strobeD;
spi_transfer_endD <= spi_transfer_end_r;
spi_transfer_end <= spi_transfer_endD;

key_strobe <= 0;
mouse_strobe <= 0;

if (~spi_transfer_endD & spi_transfer_end) begin
Expand Down Expand Up @@ -416,16 +426,29 @@ always @(posedge clk_sys) begin
if (abyte_cnt == 1) mouse_flags_r <= spi_byte_in;
else if (abyte_cnt == 2) mouse_x_r <= spi_byte_in;
else if (abyte_cnt == 3) begin
// flags: YOvfl, XOvfl, dy8, dx8, 1, mbtn, rbtn, lbtn
mouse_flags <= mouse_flags_r;
mouse_x <= mouse_x_r;
mouse_y <= spi_byte_in;
mouse_x <= { mouse_flags_r[4], mouse_x_r };
mouse_y <= { mouse_flags_r[5], spi_byte_in };
mouse_strobe <= 1;
end
end
8'h05: begin
// store incoming ps2 keyboard bytes
ps2_kbd_fifo[ps2_kbd_wptr] <= spi_byte_in;
ps2_kbd_wptr <= ps2_kbd_wptr + 1'd1;
if (abyte_cnt == 1) begin
key_extended_r <= 0;
key_pressed_r <= 1;
end
if (spi_byte_in == 8'he0) key_extended_r <= 1'b1;
else if (spi_byte_in == 8'hf0) key_pressed_r <= 1'b0;
else begin
key_extended <= key_extended_r;
key_pressed <= key_pressed_r || abyte_cnt == 1;
key_code <= spi_byte_in;
key_strobe <= 1'b1;
end
end

// joystick analog
Expand Down

0 comments on commit daf5153

Please sign in to comment.