?login_element?

Subversion Repositories NedoOS

Rev

Rev 1004 | Blame | Compare with Previous | Last modification | View Log | Download

  1.     MODULE Uart
  2. UART_DATA_REG = #c6
  3. UART_STAT_REG = #c7
  4. UART_BYTE_RECIVED = #80
  5. UART_BYTE_SENDING = #40
  6. SCANDBLCTRL_REG = #0B
  7. ZXUNO_ADDR = #FC3B
  8. ZXUNO_REG = #FD3B
  9.  
  10.  
  11. ; Enable UART
  12. ; Cleaning all flags by reading UART regs
  13. ; Wastes AF and BC
  14. init:
  15.     ld bc, ZXUNO_ADDR : ld a, UART_STAT_REG : out (c), a
  16.     ld bc, ZXUNO_REG : in A, (c)
  17.     ld bc, ZXUNO_ADDR : ld a, UART_DATA_REG : out (c), a
  18.     ld bc, ZXUNO_REG : in A, (c)
  19.     ld b, #ff
  20. .loop
  21.     push bc
  22.     call uartRead
  23.     pop bc
  24.     djnz .loop
  25.     ret
  26.  
  27. read:
  28.     call uartRead
  29.     jr nc, read
  30.     ret
  31.  
  32. ; Write single byte to UART
  33. ; A - byte to write
  34. ; BC will be wasted
  35. write:    
  36.     push af
  37.     ld bc, ZXUNO_ADDR : ld a, UART_STAT_REG : out (c), a
  38.     ld bc, ZXUNO_REG : in A, (c) : and UART_BYTE_RECIVED
  39.     jr nz, .is_recvF
  40. .checkSent
  41.     ld bc, ZXUNO_REG : in A, (c) : and UART_BYTE_SENDING
  42.     jr nz, .checkSent
  43.  
  44.     ld bc, ZXUNO_ADDR : ld a, UART_DATA_REG : out (c), a
  45.  
  46.     ld bc, ZXUNO_REG : pop af : out (c), a
  47.     ret
  48. .is_recvF
  49.     push af : push hl
  50.     ld hl, is_recv : ld a, 1 : ld (hl), a
  51.    
  52.     pop hl : pop af
  53.     jr .checkSent
  54.  
  55. ; Read byte from UART
  56. ; A: byte
  57. ; B:
  58. ;     1 - Was read
  59. ;     0 - Nothing to read
  60. uartRead:
  61.     ld a, (poked_byte) : and 1 : jr nz, .retBuff
  62.  
  63.     ld a, (is_recv) : and 1 : jr nz, recvRet
  64.  
  65.     ld bc, ZXUNO_ADDR : ld a, UART_STAT_REG : out (c), a
  66.     ld bc, ZXUNO_REG : in a, (c) : and UART_BYTE_RECIVED
  67.     jr nz, retReadByte
  68.  
  69.     or a
  70.     ret
  71. .retBuff
  72.     ld a, 0 : ld (poked_byte), a : ld a, (byte_buff)
  73.     scf
  74.     ret
  75.  
  76. retReadByte:
  77.     xor a : ld (poked_byte), a : ld (is_recv), a
  78.  
  79.     ld bc, ZXUNO_ADDR : ld a, UART_DATA_REG : out (c), a
  80.     ld bc, ZXUNO_REG : in a, (c)
  81.  
  82.     scf
  83.     ret
  84.  
  85. recvRet:
  86.     ld bc, ZXUNO_ADDR : ld a,  UART_DATA_REG : out (c),a
  87.  
  88.     ld bc, ZXUNO_REG : in a, (c)
  89.     ld hl, is_recv : ld (hl), 0
  90.     ld hl, poked_byte : ld (hl), 0
  91.    
  92.     scf
  93.     ret
  94.  
  95. poked_byte defb 0
  96. byte_buff defb 0
  97. is_recv defb 0
  98.  
  99.     ENDMODULE