Uart now has correct write implementation presumably

This commit is contained in:
Yehowshua Immanuel 2025-02-26 01:51:33 -05:00
parent 8d5cd862ab
commit 024115e389
6 changed files with 41 additions and 29 deletions

View file

@ -1,8 +1,8 @@
module Peripherals.Uart (read) where
module Peripherals.Uart (read, write, UartAddr) where
import Clash.Prelude hiding (read)
import Types (Byte)
import Data.Char (ord)
import Data.Char (ord, chr)
import Peripherals.UartCFFI (
initTerminal,
@ -20,6 +20,7 @@ import BusTypes (
ReadResponse(..),
WriteResponse(..)
)
import GHC.Generics (URec(UAddr), Generic (from))
-- based on a 16550 UART which has an address space of 8 bytes
type UartAddr = Unsigned 3
@ -28,6 +29,9 @@ type UartAddr = Unsigned 3
rbrAddr :: UartAddr
rbrAddr = 0x0
thrAddr :: UartAddr
thrAddr = 0x0
-- Line Status Register address
lsrAddr :: UartAddr
lsrAddr = 0x5
@ -50,8 +54,11 @@ buildRBR = do
-- Reads the Line Status Register (LSR) to check character availability
buildLSR :: IO Byte
buildLSR = do
char_available <- isCharAvailable
return $ fromIntegral char_available
(char_available :: Byte) <- fromIntegral <$> isCharAvailable
-- highly unlikely that we overflow stdout buffer, so we set
-- transmit to always ready
let (transmit_ready :: Byte) = 0b0010_0000
return (char_available .|. transmit_ready)
-- Updated 'read' function to handle RBR and LSR reads
read :: TransactionSize -> UartAddr -> IO BusVal
@ -59,3 +66,18 @@ read size addr
| addr == rbrAddr = busValFromByte size <$> buildRBR
| addr == lsrAddr = busValFromByte size <$> buildLSR
| otherwise = return $ busValFromByte size 0x00
extractLowestByte :: BusVal -> Byte
extractLowestByte (BusByte b) = b
extractLowestByte (BusHalfWord hw) = resize hw
extractLowestByte (BusFullWord fw) = resize fw
extractLowestByte (BusDoubleWord dw) = resize dw
extractLowestByte (BusQuadWord qw) = resize qw
byteToChar :: Byte -> Char
byteToChar = chr . fromIntegral
write :: BusVal -> UartAddr -> IO ()
write val addr
| addr == thrAddr = writeCharToTerminal $ byteToChar $ extractLowestByte val
| otherwise = return ()