OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [taskmgr/] [sys_const.s] - Rev 301

Compare with Previous | Blame | View Log

; Copyright (c)2022 Jeremy Seth Henry
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;     * Redistributions of source code must retain the above copyright
;       notice, this list of conditions and the following disclaimer.
;     * Redistributions in binary form must reproduce the above copyright
;       notice, this list of conditions and the following disclaimer in the
;       documentation and/or other materials provided with the distribution,
;       where applicable (as part of a user interface, debugging port, etc.)
;
; THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;
;------------------------------------------------------------------------------
; sys_const.s
;
; Common constants & macros for generic hardware or structures used by several
;  tasks
;
; Revision History
; Author          Date     Change
;---------------- -------- ---------------------------------------------------
; Seth Henry      7/15/22  Initial Release
;------------------------------------------------------------------------------
 
;------------------------------------------------------------------------------
;-- Serial Port Constants & Macros
;------------------------------------------------------------------------------
.DEFINE UART_DATA            0
.DEFINE UART_STATUS          1
 
.DEFINE UART_RX_PERR         3
.DEFINE UART_RX_EMPTY        4
.DEFINE UART_RX_FULL         5
.DEFINE UART_TX_EMPTY        6
.DEFINE UART_TX_FULL         7
 
.DEFINE UART_RX_PERR_BIT     2^UART_RX_PERR
.DEFINE UART_RX_PERR_MASK    UART_RX_PERR_BIT ~ $FF
 
.DEFINE UART_RX_EMPTY_BIT    2^UART_RX_EMPTY
.DEFINE UART_RX_EMPTY_MASK   UART_RX_EMPTY_BIT ~ $FF
 
.DEFINE UART_RX_FULL_BIT     2^UART_RX_FULL
.DEFINE UART_RX_FULL_MASK    UART_RX_FULL_BIT ~ $FF
 
.DEFINE UART_TX_EMPTY_BIT    2^UART_TX_EMPTY
.DEFINE UART_TX_EMPTY_MASK   UART_TX_EMPTY_BIT ~ $FF
 
.DEFINE UART_TX_FULL_BIT     2^UART_TX_FULL
.DEFINE UART_TX_FULL_MASK    UART_TX_FULL_BIT ~ $FF
 
; Checking the UART flags involves testing bits in the status register
;  The assembler isn't bright enough to use the defined constant, so this macro
;  does the test on the correct bit
 
.MACRO CHECK_UART_RX_PERR
              BTT  3
.ENDM
 
.MACRO CHECK_UART_RX_EMPTY
              BTT  4
.ENDM
 
.MACRO CHECK_UART_RX_FULL
              BTT  5
.ENDM
 
.MACRO CHECK_UART_TX_EMPTY
              BTT  6
.ENDM
 
.MACRO CHECK_UART_TX_FULL
              BTT  7
.ENDM
 
; Register Map:
; Offset  Bitfield Description                        Read/Write
;   0x00  AAAAAAAA TX Data (WR) RX Data (RD)             (RW)
;   0x01  EDCBA--- FIFO Status                           (RO*)
;                  A: RX Parity Error (write to clear)
;                  B: RX FIFO Empty
;                  C: RX FIFO almost full (922/1024)
;                  D: TX FIFO Empty
;                  E: TX FIFO almost full (922/1024)
;
;------------------------------------------------------------------------------
 
;------------------------------------------------------------------------------
; Macros for setting the return value in Cmd_Error
;------------------------------------------------------------------------------
.DEFINE RX_CERR              $FB
.DEFINE RX_TERR              $FC
.DEFINE RX_LERR              $FD
.DEFINE RX_PERR              $FE
.DEFINE RX_OKAY              $FF
 
.MACRO RETURN_RX_CMD_OKAY
              LDI  R0, #RX_OKAY
.ENDM
 
.MACRO RETURN_RX_CMD_ERROR
              LDI  R0, #RX_CERR
.ENDM
 
.MACRO RETURN_RX_TIMEOUT_ERROR
              LDI  R0, #RX_TERR
.ENDM
 
.MACRO RETURN_RX_LENGTH_ERROR
              LDI  R0, #RX_LERR
.ENDM
 
.MACRO RETURN_RX_PARITY_ERROR
              LDI  R0, #RX_PERR
.ENDM
;------------------------------------------------------------------------------
 
;------------------------------------------------------------------------------
; Common buffer/memory copy
; Uses R3:R2 as source, R5:R4 as destination, and R1 as the counter variable
;------------------------------------------------------------------------------
.MACRO MEM_COPY
__MEM_CP_LP\@:LDX  R2++
              STX  R4++
              DBNZ R1, __MEM_CP_LP\@
.ENDM
;------------------------------------------------------------------------------
 
;------------------------------------------------------------------------------
; Common buffer/memory fill/flush
; Uses R5:R4 as destination, and R1 as the counter variable
;------------------------------------------------------------------------------
.MACRO MEM_FILL
__MEM_FL_LP\@:STX  R4++
              DBNZ R1, __MEM_FL_LP\@
.ENDM
 
.MACRO MEM_FLUSH
              CLR  R0
              MEM_FILL
.ENDM
;------------------------------------------------------------------------------
 
;------------------------------------------------------------------------------
; Compute 16-bit Checksum
;
; Uses R3:R2 as source, R5:R4 as destination, and R1 as the counter variable
; Adds all of the memory locations specified to a 16-bit accumulator, returning
;  the result in R5:R4 (destination)
;------------------------------------------------------------------------------
.MACRO CALC_CHECKSUM16
              CLR  R0
              T0X  R4                   ; Initialize accumulator R5:R0 to 0
              T0X  R5
              T0X  R7                   ; Initialize R7 to 0
 
__CHK_SM_LP\@:LDX  R2++
              ADD  R4
              T0X  R4
 
              TX0  R5
              ADC  R7
              T0X  R5
 
              DBNZ R1, __CHK_SM_LP\@
.ENDM
 
; These macros assume that the pointer in R3:R2 has been left pointing to the
;  end of the receive buffer by the CALC_CHECKSUM16 macro. Do NOT repoint
; R3:R2, and be sure to use these macros in the order that the checksum bytes
;  appear in the packet. (THESE ARE ORDER DEPENDENT)
 
.MACRO CHECK_SUM_LB
              LDX  R2++
              XOR  R4
.ENDM
 
.MACRO CHECK_SUM_UB
              LDX  R2++
              XOR  R5
.ENDM
;------------------------------------------------------------------------------
 
;------------------------------------------------------------------------------
; Convert ASCII to HEX
;
; Converts an ASCII value into an integer value from 0x0 to 0xF
; Assumes incoming data is in R0. Returns a value in R0
; Returns a value of 0x00 to 0x0F for valid characters, or
; 0xFF (-1) for invalid characters
;------------------------------------------------------------------------------
.MACRO CONVERT_ASC2HEX
              PSH  R1       ; Preserve R1 and R2
              PSH  R2
 
              T0X  R2       ; Make a copy of R0 to R2 for backup
              LDI  R1, #$F0 ; Mask off the lower bits to figure out the range
              AND  R1
              T0X  R1       ; Copy the upper 4-bits to R1 for branching tests
 
              LDI  R0, #$30 ; Is it a decimal char 0-9?
              XOR  R1
              BRZ  _CNV_AH_09_\@
 
              LDI  R0, #$40 ; Is it a hex char A-F?
              XOR  R1
              BRZ  _CNV_AH_AF_\@
 
              LDI  R0, #$60 ; Is it a hex char a-f?
              XOR  R1
              BRZ  _CNV_AH_AF_\@
 
              BNI  _CNV_AH_NV_\@
 
; Valid HEX characters 0-9 are ASCII codes 0x30 to 0x39. Codes 0x3A to 0x3F are
;  invalid, so check that the value in R0 is LESS than $3A.
_CNV_AH_09_\@:TX0  R2       ; Restore R0 from backup
              LDI  R1, #$0F ; Mask off the upper bits to check validity
              AND  R1
              T0X  R2       ; Backup the lower 4-bits to R2
 
; Check for 0x0A to 0x0F
              LDI  R1, #$0A ; Load R1 with 0x0A (:)
              CMP  R1       ; Compare R0 to R1
              BNN  _CNV_AH_NV_\@  ; Branch if not negative (R0 > 9)
 
              TX0  R2       ; Restore the lower 4-bits to R0
              BNI  _CNV_AH_EX_\@
 
; Valid HEX characters A-F are 0x41 to 0x46 OR 0x61 to $66. The upper 4-bits
;  have already been checked, so just verify that the lower 4-bits are between
;  0x01 and 0x06. 0x00 and 0x07 to 0x0F are invalid,
;  so check that the value in R0 is LESS than $3A
_CNV_AH_AF_\@:TX0  R2       ; Restore R0 from backup
              LDI  R1, #$0F ; Mask off the upper bits to check validity
              AND  R1
              T0X  R2       ; Backup the lower 4-bits to R2
 
; Check for 0x00
              BRZ  _CNV_AH_NV_\@  ; 0x0 is an invalid code
 
; Check for 0x07 to 0x0F
              LDI  R1, #$07 ; Load R1 with 0x7 (G or g)
              CMP  R1       ; Compare R0 to R1
              BNN  _CNV_AH_NV_\@  ; Branch of not negative (R0 > 6)
 
; If this is a valid character, add 9 to the lower 4-bits for the result
;  (0x01 to 0x06 -> 0x0A to 0x0F)
              TX0  R2       ; Restore lower 4-bits to R0
              LDI  R1, #$09 ; Load R1 with 0x9
              ADD  R1       ; Added 9 to R0 to convert to A-F
              BNI  _CNV_AH_EX_\@
 
_CNV_AH_NV_\@:LDI  R0, #$FF ; Return 0xFF on an invalid character
 
_CNV_AH_EX_\@:POP  R2       ; Restore R1 and R2
              POP  R1
.ENDM
;------------------------------------------------------------------------------
 
;------------------------------------------------------------------------------
; Convert the lower nibble of R0 into a valid ASCII character
; Accepts data in R0 and returns the ASCII code in R0
;------------------------------------------------------------------------------
.MACRO CONVERT_NIB2ASC
              PSH  R1       ; Preserve R1
 
              LDI  R1, #$0F ; Mask away the upper 4-bits
              AND  R1
 
              T0X  R1       ; Copy to R1
              LDI  R0, #$09 ; Load R0 with 0x09
              CMP  R1       ; Compare R0 to R1
              BNN  _CNV_NA_09_\@
              ; Fall into _MC_FM_CHA_AF
 
_CNV_NA_AF_\@:LDI  R0, #$37 ; Add 0x37 to the nibble to get the ASCII value
              ADD  R1
              BNI  _CNV_NA_EX_\@
 
_CNV_NA_09_\@:LDI  R0, #$30 ; Add 0x30 to the nibble to get the ASCII value
              OR   R1
              ; Fall into _MC_FM_CHA_EX
 
_CNV_NA_EX_\@:POP  R1
.ENDM
;------------------------------------------------------------------------------
 
;------------------------------------------------------------------------------
; Convert the packed BCD value in R0 to its ASCII equivalent in R3:R2
; $59 -> $35, $39
;------------------------------------------------------------------------------
.MACRO CONV_PBCD_TO_ASCII
              T0X  R2        ; Copy R0 -> R2 for re-use
              LDI  R0, #$F0  ; Set the upper nibble mask. BCD is only 3-bit
              AND  R2        ; Mask off the upper 3-bits of value -> R0
              CLP  PSR_C     ; Clear the carry (in case CPU options not set)
              ROR  R0        ; Shift the upper nibble down
              ROR  R0
              ROR  R0
              ROR  R0
              LDI  R1, #$30  ; Set the ASCII upper nibble mask of $30
              OR   R1        ; Create an ASCII character
              T0X  R3        ; Transfer the upper character to R3
 
              LDI  R0, #$0F  ; Set the lower nibble mask
              AND  R2        ; Mask off the lower bits
              OR   R1        ; Create an ASCII character
              T0X  R2        ; Store the lower character to R1
              RTS
.ENDM
;------------------------------------------------------------------------------
 

Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.