2021-01-23 15:19:09 +00:00
///////////////////////////////////////////
// uart.sv
//
// Written: David_Harris@hmc.edu 21 January 2021
// Modified:
//
// Purpose: Interface to Universial Asynchronous Receiver/ Transmitter with FIFOs
// Emulates interface of Texas Instruments PC165550D
// Compatible with UART in Imperas Virtio model ***
//
// A component of the Wally configurable RISC-V project.
//
// Copyright (C) 2021 Harvey Mudd College & Oklahoma State University
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
// OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
///////////////////////////////////////////
2021-01-23 15:48:12 +00:00
`include " wally-config.vh "
2021-01-23 15:19:09 +00:00
2021-01-23 15:48:12 +00:00
module uart (
2021-02-02 04:44:41 +00:00
input logic HCLK , HRESETn ,
2021-03-05 19:24:22 +00:00
input logic HSELUART ,
input logic [ 2 : 0 ] HADDR ,
input logic HWRITE ,
2021-01-30 04:43:48 +00:00
input logic [ `XLEN - 1 : 0 ] HWDATA ,
output logic [ `XLEN - 1 : 0 ] HREADUART ,
output logic HRESPUART , HREADYUART ,
2021-02-02 04:44:41 +00:00
input logic SIN , DSRb , DCDb , CTSb , RIb , // from E1A driver from RS232 interface
output logic SOUT , RTSb , DTRb , // to E1A driver to RS232 interface
output logic OUT1b , OUT2b , INTR , TXRDYb , RXRDYb ) ; // to CPU
2021-01-23 15:19:09 +00:00
// UART interface signals
logic [ 2 : 0 ] A ;
2021-04-14 14:19:42 +00:00
logic MEMRb , MEMWb , memread , memwrite ;
2021-01-23 15:19:09 +00:00
logic [ 7 : 0 ] Din , Dout ;
// rename processor interface signals to match PC16550D and provide one-byte interface
2021-04-14 14:19:42 +00:00
flopr # ( 1 ) memreadreg ( HCLK , ~ HRESETn , ( HSELUART & ~ HWRITE ) , memread ) ;
flopr # ( 1 ) memwritereg ( HCLK , ~ HRESETn , ( HSELUART & HWRITE ) , memwrite ) ;
2021-03-22 19:40:29 +00:00
flopr # ( 3 ) haddrreg ( HCLK , ~ HRESETn , HADDR [ 2 : 0 ] , A ) ;
2021-04-14 14:19:42 +00:00
assign MEMRb = ~ memread ;
assign MEMWb = ~ memwrite ;
2021-03-22 19:40:29 +00:00
2021-01-30 04:43:48 +00:00
assign HRESPUART = 0 ; // OK
2021-03-05 19:24:22 +00:00
assign HREADYUART = 1 ; // should idle high during address phase and respond high when done; will need to be modified if UART ever needs more than 1 cycle to do something
2021-01-23 15:19:09 +00:00
generate
2021-01-23 15:48:12 +00:00
if ( `XLEN = = 64 ) begin
2021-03-05 19:24:22 +00:00
always_comb begin
2021-01-30 04:43:48 +00:00
HREADUART = { Dout , Dout , Dout , Dout , Dout , Dout , Dout , Dout } ;
2021-03-05 19:24:22 +00:00
case ( A )
3 'b000 : Din = HWDATA [ 7 : 0 ] ;
3 'b001 : Din = HWDATA [ 15 : 8 ] ;
3 'b010 : Din = HWDATA [ 23 : 16 ] ;
3 'b011 : Din = HWDATA [ 31 : 24 ] ;
3 'b100 : Din = HWDATA [ 39 : 32 ] ;
3 'b101 : Din = HWDATA [ 47 : 40 ] ;
3 'b110 : Din = HWDATA [ 55 : 48 ] ;
3 'b111 : Din = HWDATA [ 63 : 56 ] ;
2021-01-25 16:28:43 +00:00
endcase
2021-01-23 15:19:09 +00:00
end
end else begin // 32-bit
2021-03-05 19:24:22 +00:00
always_comb begin
2021-01-30 04:43:48 +00:00
HREADUART = { Dout , Dout , Dout , Dout } ;
2021-03-05 19:24:22 +00:00
case ( A [ 1 : 0 ] )
2 'b00 : Din = HWDATA [ 7 : 0 ] ;
2 'b01 : Din = HWDATA [ 15 : 8 ] ;
2 'b10 : Din = HWDATA [ 23 : 16 ] ;
2 'b11 : Din = HWDATA [ 31 : 24 ] ;
2021-01-23 15:19:09 +00:00
endcase
end
end
endgenerate
logic BAUDOUTb ; // loop tx clock BAUDOUTb back to rx clock RCLK
2021-01-30 05:56:12 +00:00
// *** make sure reads don't occur on UART unless fully selected because they could change state. This applies to all peripherals
2021-01-23 15:19:09 +00:00
uartPC16550D u ( . RCLK ( BAUDOUTb ) , . * ) ;
endmodule