1 module MulDivDCM(input xtal, output clk);
7 BUFG CLKFX_BUFG_INST (.I(CLKFX_BUF),
9 DCM_SP DCM_SP_INST (.CLKFB(GND_BIT),
17 defparam DCM_SP_INST.CLK_FEEDBACK = "NONE";
18 defparam DCM_SP_INST.CLKDV_DIVIDE = 2.0;
19 defparam DCM_SP_INST.CLKFX_DIVIDE = div;
20 defparam DCM_SP_INST.CLKFX_MULTIPLY = mul;
21 defparam DCM_SP_INST.CLKIN_DIVIDE_BY_2 = "FALSE";
22 defparam DCM_SP_INST.CLKIN_PERIOD = 20.000;
23 defparam DCM_SP_INST.CLKOUT_PHASE_SHIFT = "NONE";
24 defparam DCM_SP_INST.DESKEW_ADJUST = "SYSTEM_SYNCHRONOUS";
25 defparam DCM_SP_INST.DFS_FREQUENCY_MODE = "LOW";
26 defparam DCM_SP_INST.DLL_FREQUENCY_MODE = "LOW";
27 defparam DCM_SP_INST.DUTY_CYCLE_CORRECTION = "TRUE";
28 defparam DCM_SP_INST.FACTORY_JF = 16'hC080;
29 defparam DCM_SP_INST.PHASE_SHIFT = 0;
30 defparam DCM_SP_INST.STARTUP_WAIT = "TRUE";
37 output reg [2:0] green,
38 output reg [1:0] blue,
48 MulDivDCM dcm25(xtal, clk25);
49 defparam dcm25.div = 4;
50 defparam dcm25.mul = 2;
52 SyncGen sync(clk25, vs, hs, x, y, border);
72 CharSet cs(cschar, csrow, csdata);
73 VideoRAM vram(clk25, vraddr + vscroll, vrdata, vwaddr, vwdata, vwr);
74 VDisplay dpy(clk25, x, y, vraddr, vrdata, cschar, csrow, csdata, odata);
75 SerRX rx(clk25, serwr, serdata, serrx);
76 SerTX tx(clk25, sertxwr, sertxdata, sertx);
77 RXState rxsm(clk25, vwr, vwaddr, vwdata, vscroll, serwr, serdata);
78 PS2 ps2(clk25, ps2c, ps2d, sertxwr, sertxdata);
80 always @(posedge clk25) begin
81 red <= border ? 0 : {3{odata}};
82 green <= border ? 0 : {3{odata}};
83 blue <= border ? 0 : {2{odata}};
90 output reg [11:0] x, y,
94 parameter XFPORCH = 16;
96 parameter XBPORCH = 48;
99 parameter YFPORCH = 10;
101 parameter YBPORCH = 29;
103 always @(posedge pixclk)
105 if (x >= (XRES + XFPORCH + XSYNC + XBPORCH))
107 if (y >= (YRES + YFPORCH + YSYNC + YBPORCH))
114 hs <= (x >= (XRES + XFPORCH)) && (x < (XRES + XFPORCH + XSYNC));
115 vs <= (y >= (YRES + YFPORCH)) && (y < (YRES + YFPORCH + YSYNC));
116 border <= (x > XRES) || (y > YRES);
123 output wire [7:0] data);
125 reg [7:0] rom [(256 * 8 - 1):0];
128 $readmemb("ibmpc1.mem", rom);
130 assign data = rom[{char, row}];
136 output reg [7:0] rdata,
141 reg [7:0] ram [2047 : 0];
143 always @(posedge pixclk)
146 always @(posedge pixclk)
155 output wire [10:0] raddr,
157 output wire [7:0] cschar,
158 output wire [2:0] csrow,
162 wire [7:0] col = x[11:3];
163 wire [5:0] row = y[9:3];
167 assign raddr = ({row,4'b0} + {row,6'b0} + {4'h0,col});
168 assign cschar = rchar;
169 assign csrow = y[2:0];
171 always @(posedge pixclk)
174 always @(posedge pixclk)
175 data = ((xdly < 80 * 8) && (y < 25 * 8)) ? csdata[7 - xdly[2:0]] : 0;
178 `define IN_CLK 25000000
179 `define OUT_CLK 57600
180 `define CLK_DIV (`IN_CLK / `OUT_CLK)
185 output reg [7:0] wchar = 0,
188 reg [15:0] rx_clkdiv = 0;
189 reg [3:0] rx_state = 4'b0000;
190 reg [7:0] rx_data_tmp;
193 always @(posedge pixclk)
195 if ((rx_state == 0) && (serialrx == 0) /*&& (rx_hasdata == 0)*/) /* Kick off. */
197 else if ((rx_state != 4'b0000) && (rx_clkdiv == 0)) begin
198 if (rx_state != 4'b1010)
199 rx_state <= rx_state + 1;
203 4'b0001: begin end /* Twiddle thumbs -- this is the end of the half bit. */
204 4'b0010: rx_data_tmp[0] <= serialrx;
205 4'b0011: rx_data_tmp[1] <= serialrx;
206 4'b0100: rx_data_tmp[2] <= serialrx;
207 4'b0101: rx_data_tmp[3] <= serialrx;
208 4'b0110: rx_data_tmp[4] <= serialrx;
209 4'b0111: rx_data_tmp[5] <= serialrx;
210 4'b1000: rx_data_tmp[6] <= serialrx;
211 4'b1001: rx_data_tmp[7] <= serialrx;
212 4'b1010: if (serialrx == 1) begin
214 wchar <= rx_data_tmp;
222 if ((rx_state == 0) && (serialrx == 0) /*&& (rx_hasdata == 0)*/) /* Wait half a period before advancing. */
223 rx_clkdiv <= `CLK_DIV / 2 + `CLK_DIV / 4;
224 else if (rx_clkdiv == `CLK_DIV)
227 rx_clkdiv <= rx_clkdiv + 1;
235 output reg serial = 1);
237 reg [7:0] tx_data = 0;
238 reg [15:0] tx_clkdiv = 0;
239 reg [3:0] tx_state = 4'b0000;
241 wire tx_newdata = wr && !tx_busy;
243 always @(posedge pixclk)
249 end else if (tx_clkdiv == 0) begin
250 tx_state <= tx_state + 1;
253 4'b0000: serial <= 0;
254 4'b0001: serial <= tx_data[0];
255 4'b0010: serial <= tx_data[1];
256 4'b0011: serial <= tx_data[2];
257 4'b0100: serial <= tx_data[3];
258 4'b0101: serial <= tx_data[4];
259 4'b0110: serial <= tx_data[5];
260 4'b0111: serial <= tx_data[6];
261 4'b1000: serial <= tx_data[7];
262 4'b1001: serial <= 1;
263 4'b1010: tx_busy <= 0;
268 if(tx_newdata || (tx_clkdiv == `CLK_DIV))
271 tx_clkdiv <= tx_clkdiv + 1;
278 output reg [10:0] vwaddr = 0,
279 output reg [7:0] vwdata = 0,
280 output reg [10:0] vscroll = 0,
282 input [7:0] serdata);
284 parameter STATE_IDLE = 4'b0000;
285 parameter STATE_NEWLINE = 4'b0001;
286 parameter STATE_CLEAR = 4'b0010;
288 reg [3:0] state = STATE_CLEAR;
293 reg [10:0] clearstart = 0;
294 reg [10:0] clearend = 11'b11111111111;
296 always @(posedge clk25)
298 STATE_IDLE: if (serwr) begin
299 if (serdata == 8'h0A) begin
300 state <= STATE_NEWLINE;
302 end else if (serdata == 8'h0D) begin
305 end else if (serdata == 8'h0C) begin
307 clearend <= 11'b11111111111;
311 state <= STATE_CLEAR;
314 vwaddr <= ({y,4'b0} + {y,6'b0} + {4'h0,x}) + vscroll;
318 state <= STATE_NEWLINE;
327 vscroll <= vscroll + 80;
328 clearstart <= (25 * 80) + vscroll;
329 clearend <= (26*80) + vscroll;
330 state <= STATE_CLEAR;
339 vwaddr <= clearstart;
341 clearstart <= clearstart + 1;
342 if (clearstart == clearend)
353 output reg [7:0] data
356 reg [3:0] bitcount = 0;
358 reg keyarrow = 0, keyup = 0, parity = 0;
361 /* Clock debouncing */
363 reg [6:0] debounce = 0;
365 reg [11:0] resetcountdown = 0;
367 reg [6:0] unshiftedrom [127:0]; initial $readmemh("scancodes.unshifted.hex", unshiftedrom);
368 reg [6:0] shiftedrom [127:0]; initial $readmemh("scancodes.shifted.hex", shiftedrom);
372 reg mod_capslock = 0;
373 wire mod_shifted = (mod_lshift | mod_rshift) ^ mod_capslock;
378 always @(posedge pixclk) begin
379 if (inclk != lastinclk) begin
382 resetcountdown <= 12'b111111111111;
383 end else if (debounce == 0) begin
385 resetcountdown <= resetcountdown - 1;
387 debounce <= debounce + 1;
389 if (nd ^ lastnd) begin
396 always @(negedge fixedclk) begin
397 if (resetcountdown == 0)
399 else if (bitcount == 10) begin
401 if(parity != (^ key)) begin
405 8'hxx: keyarrow <= 0;
413 8'h12: mod_lshift <= 0;
414 8'h59: mod_rshift <= 0;
416 // handle this? I don't fucking know
420 8'hE0: keyarrow <= 1; // handle these? I don't fucking know
422 8'h12: mod_lshift <= 1;
423 8'h59: mod_rshift <= 1;
424 8'h14: mod_capslock <= ~mod_capslock;
425 8'b0xxxxxxx: begin nd <= ~nd; data <= mod_shifted ? shiftedrom[key] : unshiftedrom[key]; end
426 8'b1xxxxxxx: begin /* AAAAAAASSSSSSSS */ end
436 bitcount <= bitcount + 1;