module U3_CPLD_GPIO(
//Port List
CPLD_CLK ,
CPLD_RESET ,
PMAD ,
PMCS0 ,
PMRD ,
PMWR ,
LCD_BL ,
LCD_E ,
LCD_RS ,
LCD_RW ,
LCD_D ,
PMD ,
INT1 ,
INT2 ,
IO_DIR ,
LED
);
//Input declaration
input CPLD_CLK;
input CPLD_RESET;
input PMAD;
input PMCS0;
input PMRD;
input PMWR;
//LCD
output LCD_BL;
output LCD_E;
output LCD_RS;
output LCD_RW;
//LCD IO Config
reg [7:0] WriteData;
inout wire[7:0] PMD; //LCD data to/from PIC
inout wire[7:0] LCD_D; //Data to/from LCD
//INT
output INT1;
output INT2;
input [3:0]IO_DIR;
//LED
output reg LED;
//Port Master Signals
reg [7:0] PMD_Regd;
reg [7:0] PMD_Out;
reg PMWR_Regd;
reg PMRD_Regd;
reg PMCS0_Regd;
reg PMAD_Regd;
assign LCD_BL = 1'b1;
//LED Blink
reg [25:0] counter;
always @(posedge CPLD_CLK)
if (counter == 4000000) //4MHz
begin
counter <= 0;
LED <= ~LED;
end
else
begin
counter <= counter + 1;
LED <= LED;
end
//LCD Process
always @(posedge CPLD_CLK)
if (PMWR == 1'b0)
begin
PMD_Regd [7:0] <= PMD [7:0]; //Read LCD data from PIC and save it to PMD_Regd
PMWR_Regd <= PMWR;
PMRD_Regd <= PMRD;
PMCS0_Regd <= PMCS0;
PMAD_Regd <= PMAD; //LCD_E
LCD_RS <= PMRD_Regd;
LCD_RW <= PMWR_Regd;
LCD_E <= PMAD_Regd;
LCD_D [7:0] <= PMD_Regd [7:0]; //Send LCD data from PMD_Regd to LCD
end
else
begin
PMWR_Regd <= PMWR;
PMRD_Regd <= PMRD;
PMD_Regd [7:0] <= LCD_D [7:0]; //Read data from LCD and save it to PMD_Regd
PMD [7:0] <= PMD_Regd [7:0]; //Send LCD data from PMD_Regd to PIC
end
endmodule