always @(posedge clk or negedge rst) if(!rst) current_state<=NO_KEY_PRESSED; else current_state<=next_state;
always @(posedge clk0) begin case(current_state) NO_KEY_PRESSED: if(row!=4'b1111) next_state=SCAN_COL0; else begin next_state=NO_KEY_PRESSED;end SCAN_COL0: begin if(row!=4'b1111) next_state=KEY_PRESSED; else next_state=SCAN_COL1;
end SCAN_COL1: begin if(row!=4'b1111) next_state=KEY_PRESSED; else next_state=SCAN_COL2; end SCAN_COL2: begin if(row!=4'b1111) next_state=KEY_PRESSED; else next_state=SCAN_COL3; end SCAN_COL3: begin if(row!=4'b1111) next_state=KEY_PRESSED; else next_state=NO_KEY_PRESSED; end KEY_PRESSED: if(row!=4'b1111) next_state=KEY_PRESSED; else next_state=NO_KEY_PRESSED; default: next_state=NO_KEY_PRESSED; endcase end
always @ (posedge clk or negedge rst) begin if (!rst) begin col<= 4'b0000; key_pressed_flag<=0; end else case(next_state) NO_KEY_PRESSED:begin col=4'b0000;key_pressed_flag=0;end SCAN_COL0: col=4'b1110; SCAN_COL1: col=4'b1101; SCAN_COL2: col=4'b1011; SCAN_COL3: col=4'b0111; KEY_PRESSED: begin col_reg=col;row_reg=row; key_pressed_flag=1; end endcase end