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);
75 CharSet cs(cschar, csrow, csdata);
76 VideoRAM vram(clk25, vraddr + vscroll, vrdata, vwaddr, vwdata, vwr);
77 VDisplay dpy(clk25, x, y, vraddr, vrdata, cschar, csrow, csdata, vcursx, vcursy, odata);
78 SerRX rx(clk25, serwr, serdata, serrx);
79 SerTX tx(clk25, sertxwr, sertxdata, sertx);
80 RXState rxsm(clk25, vwr, vwaddr, vwdata, vscroll, vcursx, vcursy, serwr, serdata);
81 PS2 ps2(clk25, ps2c, ps2d, sertxwr, sertxdata);
83 always @(posedge clk25) begin
84 red <= border ? 0 : {3{odata}};
85 green <= border ? 0 : {3{odata}};
86 blue <= border ? 0 : {2{odata}};
93 output reg [11:0] x, y,
97 parameter XFPORCH = 16;
99 parameter XBPORCH = 48;
101 parameter YRES = 480;
102 parameter YFPORCH = 10;
104 parameter YBPORCH = 29;
106 always @(posedge pixclk)
108 if (x >= (XRES + XFPORCH + XSYNC + XBPORCH))
110 if (y >= (YRES + YFPORCH + YSYNC + YBPORCH))
117 hs <= (x >= (XRES + XFPORCH)) && (x < (XRES + XFPORCH + XSYNC));
118 vs <= (y >= (YRES + YFPORCH)) && (y < (YRES + YFPORCH + YSYNC));
119 border <= (x > XRES) || (y > YRES);
126 output wire [7:0] data);
128 reg [7:0] rom [(256 * 8 - 1):0];
131 $readmemb("ibmpc1.mem", rom);
133 assign data = rom[{char, row}];
139 output reg [7:0] rdata,
144 reg [7:0] ram [2047 : 0];
146 always @(posedge pixclk)
149 always @(posedge pixclk)
158 output wire [10:0] raddr,
160 output wire [7:0] cschar,
161 output wire [2:0] csrow,
167 wire [7:0] col = x[11:3];
168 wire [5:0] row = y[9:3];
172 assign raddr = ({row,4'b0} + {row,6'b0} + {4'h0,col});
173 assign cschar = rchar;
174 assign csrow = y[2:0];
176 reg [23:0] blinktime = 0;
178 always @(posedge pixclk) blinktime <= blinktime + 1;
180 wire curssel = (cursx == col) && (cursy == row) && blinktime[23];
182 always @(posedge pixclk)
185 always @(posedge pixclk)
186 data = ((xdly < 80 * 8) && (y < 25 * 8)) ? (csdata[7 - xdly[2:0]] ^ curssel) : 0;
189 `define IN_CLK 25000000
190 `define OUT_CLK 57600
191 `define CLK_DIV (`IN_CLK / `OUT_CLK)
196 output reg [7:0] wchar = 0,
199 reg [15:0] rx_clkdiv = 0;
200 reg [3:0] rx_state = 4'b0000;
201 reg [7:0] rx_data_tmp;
204 always @(posedge pixclk)
206 if ((rx_state == 0) && (serialrx == 0) /*&& (rx_hasdata == 0)*/) /* Kick off. */
208 else if ((rx_state != 4'b0000) && (rx_clkdiv == 0)) begin
209 if (rx_state != 4'b1010)
210 rx_state <= rx_state + 1;
214 4'b0001: begin end /* Twiddle thumbs -- this is the end of the half bit. */
215 4'b0010: rx_data_tmp[0] <= serialrx;
216 4'b0011: rx_data_tmp[1] <= serialrx;
217 4'b0100: rx_data_tmp[2] <= serialrx;
218 4'b0101: rx_data_tmp[3] <= serialrx;
219 4'b0110: rx_data_tmp[4] <= serialrx;
220 4'b0111: rx_data_tmp[5] <= serialrx;
221 4'b1000: rx_data_tmp[6] <= serialrx;
222 4'b1001: rx_data_tmp[7] <= serialrx;
223 4'b1010: if (serialrx == 1) begin
225 wchar <= rx_data_tmp;
233 if ((rx_state == 0) && (serialrx == 0) /*&& (rx_hasdata == 0)*/) /* Wait half a period before advancing. */
234 rx_clkdiv <= `CLK_DIV / 2 + `CLK_DIV / 4;
235 else if (rx_clkdiv == `CLK_DIV)
238 rx_clkdiv <= rx_clkdiv + 1;
246 output reg serial = 1);
248 reg [7:0] tx_data = 0;
249 reg [15:0] tx_clkdiv = 0;
250 reg [3:0] tx_state = 4'b0000;
252 wire tx_newdata = wr && !tx_busy;
254 always @(posedge pixclk)
260 end else if (tx_clkdiv == 0) begin
261 tx_state <= tx_state + 1;
264 4'b0000: serial <= 0;
265 4'b0001: serial <= tx_data[0];
266 4'b0010: serial <= tx_data[1];
267 4'b0011: serial <= tx_data[2];
268 4'b0100: serial <= tx_data[3];
269 4'b0101: serial <= tx_data[4];
270 4'b0110: serial <= tx_data[5];
271 4'b0111: serial <= tx_data[6];
272 4'b1000: serial <= tx_data[7];
273 4'b1001: serial <= 1;
274 4'b1010: tx_busy <= 0;
279 if(tx_newdata || (tx_clkdiv == `CLK_DIV))
282 tx_clkdiv <= tx_clkdiv + 1;
289 output reg [10:0] vwaddr = 0,
290 output reg [7:0] vwdata = 0,
291 output reg [10:0] vscroll = 0,
292 output wire [6:0] vcursx,
293 output wire [4:0] vcursy,
295 input [7:0] serdata);
297 parameter STATE_IDLE = 4'b0000;
298 parameter STATE_NEWLINE = 4'b0001;
299 parameter STATE_CLEAR = 4'b0010;
301 reg [3:0] state = STATE_CLEAR;
309 reg [10:0] clearstart = 0;
310 reg [10:0] clearend = 11'b11111111111;
312 always @(posedge clk25)
314 STATE_IDLE: if (serwr) begin
315 if (serdata == 8'h0A) begin
316 state <= STATE_NEWLINE;
319 end else if (serdata == 8'h0D) begin
322 end else if (serdata == 8'h0C) begin
324 clearend <= 11'b11111111111;
328 state <= STATE_CLEAR;
329 end else if (serdata == 8'h08) begin
335 vwaddr <= ({y,4'b0} + {y,6'b0} + {4'h0,x}) + vscroll;
339 state <= STATE_NEWLINE;
348 vscroll <= vscroll + 80;
349 clearstart <= (25 * 80) + vscroll;
350 clearend <= (26*80) + vscroll;
351 state <= STATE_CLEAR;
360 vwaddr <= clearstart;
362 clearstart <= clearstart + 1;
363 if (clearstart == clearend)
374 output reg [7:0] data
377 reg [3:0] bitcount = 0;
379 reg keyarrow = 0, keyup = 0, parity = 0;
382 /* Clock debouncing */
384 reg [6:0] debounce = 0;
386 reg [11:0] resetcountdown = 0;
388 reg [6:0] unshiftedrom [127:0]; initial $readmemh("scancodes.unshifted.hex", unshiftedrom);
389 reg [6:0] shiftedrom [127:0]; initial $readmemh("scancodes.shifted.hex", shiftedrom);
393 reg mod_capslock = 0;
394 wire mod_shifted = (mod_lshift | mod_rshift) ^ mod_capslock;
399 always @(posedge pixclk) begin
400 if (inclk != lastinclk) begin
403 resetcountdown <= 12'b111111111111;
404 end else if (debounce == 0) begin
406 resetcountdown <= resetcountdown - 1;
408 debounce <= debounce + 1;
410 if (nd ^ lastnd) begin
417 always @(negedge fixedclk) begin
418 if (resetcountdown == 0)
420 else if (bitcount == 10) begin
422 if(parity != (^ key)) begin
426 8'hxx: keyarrow <= 0;
434 8'h12: mod_lshift <= 0;
435 8'h59: mod_rshift <= 0;
437 // handle this? I don't fucking know
441 8'hE0: keyarrow <= 1; // handle these? I don't fucking know
443 8'h12: mod_lshift <= 1;
444 8'h59: mod_rshift <= 1;
445 8'h14: mod_capslock <= ~mod_capslock;
446 8'b0xxxxxxx: begin nd <= ~nd; data <= mod_shifted ? shiftedrom[key] : unshiftedrom[key]; end
447 8'b1xxxxxxx: begin /* AAAAAAASSSSSSSS */ end
457 bitcount <= bitcount + 1;