You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.0 KiB

module top (input CLK, output [2:0] DBG_LED, output DBG_TX, input I2C_SCL, inout I2C_SDA, inout INTERRUPT, inout [33:0] KBD_RC,
output NUM_LOCK, output CAPS_LOCK, output SCROOL_LOCK);
//parameter INTERRUPT_TMR_REFLESH = 14; // 14 - 2^14=16384 tacts or 1.37 ms, 19 - 2^19=524288 tacts or 43.7 ms, 23 - 2^23=8388608 tacts or 0.7 s
// 23 - 1119 LCs, 14 - 1081 LCs (in commit 1b6fc60221b595c2a0f69509d29b6e5c3110feb0)
// reg [21:0] tact_counter_2ms = 0;
// reg [5:0] ms_counter = 0;
// reg [7:0] s_counter = 0;
reg [16:0] tact_counter;
reg [5:0] rc_counter;
reg [33:0] KBD_RC_OUT = 0;
reg [33:0] KBD_RC_EN = 1;
wire [33:0] KBD_RC_IN;
// reg [5:0] i;
reg [33:0] com_msg;
reg com_en;
wire RESET;
reg [3:0] rststate = 0;
assign RESET = &rststate;
reg UART_WR, UART_DTR, UART_RTS, UART_DCD;//, UART_WR2;
reg [7:0] UART_TX_DATA;
wire UART_ACTIVE, UART_TX_LINE;
uart UART (CLK, RESET, UART_WR, UART_TX_DATA, UART_ACTIVE, UART_TX_LINE);
reg [2:0] dbg_led = 0;
always @(posedge CLK) begin
// RESET LOGIC
rststate <= rststate + !RESET;
if (RESET == 0) begin
tact_counter_2ms = 0;
ms_counter = 0;
s_counter = 0;
dbg_led = 1;
com_msg[5:0] = 63;
com_len = 1;
com_en = 0;
end
// NOT RESET MODE LOGIC
else begin
if (tact_counter == 0)
KBD_RC_EN = 1 << rc_counter;
tact_counter = tact_counter + 1;
if (tact_counter == 120000) begin
tact_counter = 0;
if ((KBD_RC_IN | KBD_RC_EN) != 34'h 3FFFFFFFF) begin
com_msg = KBD_RC_IN ^ (34'h 3FFFFFFFF);
com_en = 1;
end
else
com_en = 0;
rc_counter = rc_counter + 1;
if (rc_counter == 33) begin
rc_counter = 0;
dbg_led[1:0] <= dbg_led[2:1];
dbg_led[2] <= dbg_led[0];
end
end
if (tact_counter[10:0] == 0) begin
if (com_en) begin
case (tact_counter[14:11])
0: UART_TX_DATA = 255;
1: UART_TX_DATA[1:0] = com_msg[33:32];
2: UART_TX_DATA = com_msg[31:24];
3: UART_TX_DATA = com_msg[23:16];
4: UART_TX_DATA = com_msg[15:8];
5: UART_TX_DATA = com_msg[7:0];
endcase;
if (tact_counter[14:11] == 1)
UART_TX_DATA[7:2] = 0;
if (tact_counter[14:11] == 5)
com_en = 0;
UART_WR = 1;
end
else if ((tact_counter[16:14] == 1) && (rc_counter == 0) && (tact_counter[13:11] < 6)) begin
case (tact_counter[13:11])
0: UART_TX_DATA = 255;
1: UART_TX_DATA = 0;
2: UART_TX_DATA = 0;
3: UART_TX_DATA = 0;
4: UART_TX_DATA = 0;
5: UART_TX_DATA = 0;
endcase;
UART_WR = 1;
end
end
else if (tact_counter[10:0] == 2)
UART_WR = 0;
// //tact_counter_2ms = tact_counter_2ms + 1;
// if (tact_counter_2ms == 3999999) begin
// tact_counter_2ms = 0;
// ms_counter = ms_counter + 1;
// if (ms_counter == 0)
// dbg_led = dbg_led ^ 7;
// end
// else
// tact_counter_2ms = tact_counter_2ms + 1;
//
// if ((tact_counter_2ms == 3999999) && (ms_counter < 33)) begin
// if (KBD_RC_IN != 34'h 3FFFF) begin
// if (com_len == 1) begin
// com_msg[11:6] = ms_counter;
// com_len = com_len + 1;
// // for (i = 0; i < 34; i = i + 1) begin
// // if (KBD_RC_IN[i] == 0) begin
// // if (com_len != 7) begin
// // com_msg[(com_len*6+5):(com_len*6)] = i;
// // com_len = com_len + 1;
// // end
// // else
// // com_msg[47:42] = 62;
// // end
// // end
// end
// end
// end
//
// if (ms_counter == 34) begin
// if ((tact_counter_2ms & 2047) == 0) begin
// if (com_len != 1) begin
// if (tact_counter_2ms[13:11] < com_len) begin
// UART_TX_DATA[5:0] = com_msg[(6 * tact_counter_2ms[13:11] + 5) : (6 * tact_counter_2ms[13:11] + 0)];
// end
// end
// end
// else if ((tact_counter_2ms & 2047) == 1024)
// UART_WR = 0;
// // if ((tact_counter_2ms % 1200) == 0) begin
// // i = tact_counter_2ms / 1200;
// // if (com_len != 1) begin
// // if (i < com_len) begin
// // UART_TX_DATA[5:0] = com_msg[(6 * i + 5) : (6 * i + 0)];
// // UART_WR = 1;
// // end
// // end
// // end
// // else if ((tact_counter_2ms % 1200) == 600)
// // UART_WR = 0;
// end
//
// if (ms_counter == 35)
// com_len = 1;
//
// if (ms_counter < 33)
// KBD_RC_EN = 1 << ms_counter;
// else
// KBD_RC_EN = 0;
end
end
assign DBG_LED = dbg_led;//tact_counter_2ms[21:19];
assign NUM_LOCK = dbg_led[0];
assign CAPS_LOCK = dbg_led[1];
assign SCROOL_LOCK = dbg_led[2];
//assign DBG_LED[2] = KBD_RC_IN[23];
assign DBG_TX = UART_TX_LINE;
// assign LED5 = I2C_TRANS;
// //assign LED5 = COM_RX;
// assign LED1 = INT ^ 1;//KBD_COLUMNS[0];//I2C_OUTPUT_TYPE[0];//I2C_RX[0];
// //assign LED2 = I2C_OUTPUT_TYPE[0];
// //assign LED3 = I2C_OUTPUT_TYPE[1];
// assign LED2 = KBD_LED_STATUS[0];
// assign LED3 = KBD_LED_STATUS[1];
// assign LED4 = KBD_LED_STATUS[2];//KBD_FREEZE;//UART_ACTIVE;
// //assign LED3 = UART_ACTIVE;
// //assign LED4 = uart_double_ff;
// //assign ACK = I2C_READ;//I2C_WR; //I2C_ACK;
// //assign LED2 = KBD_DBG;
//
// assign COM_TX = UART_TX_LINE;//COM_RX;
// //assign INTERRUPT = INT;
// assign COM_RTS = I2C_READ;//UART_RTS;
// assign COM_DSR = KBD_FREEZE;//UART_DTR;
// assign COM_DCD = INT;
SB_IO #(
.PIN_TYPE(6'b 1010_01),
.PULLUP(1'b 1)
) kbd_rc_io [33:0] (
.PACKAGE_PIN(KBD_RC),
.OUTPUT_ENABLE(KBD_RC_EN),
.D_OUT_0(KBD_RC_OUT),
.D_IN_0(KBD_RC_IN)
);
endmodule //top