;----------------------------------------------------------------------
;
;  8049SPY.ASM
;
;  This program reads the internal ROM in a 8048 or 8049 and sends it
;  serially to a PC in the Intel HEX format at 9600 baud, 8 data bits,
;  no parity and 1 stop bit.
;
;  No matter what the actual ROM size is, the program always sends 4kb
;  to the computer, which is the maximum address range of the 8048
;  series of processors.
;
;  Author: San Bergmans
;  Date  : 16-5-1994
;  www.sbprojects.com
;
;  Program written for the 6802 Nano Computer with the SB-Assembler
;
;----------------------------------------------------------------------

                .CR     6800
                .OR     $F000
                .TF     8049SPY.INT,INT,32

;----------------------------------------------------------------------
;  Constants and declarations
;----------------------------------------------------------------------

CR              .EQ     $0D             Carriage return

ADDRESS         .EQ     $0000,$0001     Address in 8048 ROM
COUNTER         .EQ     $0002           Byte counter in line
CHCKSUM         .EQ     $0003           Checksum

PIA_A           .EQ     $0080           Pia data register A
PIA_B           .EQ     $0081           Pia data register B
CON_A           .EQ     $0082           Pia control register A
CON_B           .EQ     $0083           Pia control register B

;----------------------------------------------------------------------
;  SCHOUT
;  Send character in A to the serial output at 9600 baud
;----------------------------------------------------------------------

SCHOUT          PSHA                    Save affected registers
                PSHB
                LDAB    #9              Setup bit counter
                CLC                     Make a start bit
                CLI                     Disable interrupts

.LOOP           PSHB                    Save bit counter
                LDAB    PIA_B
                BCC     .BITLOW         Send low bit!
                ORAB    #%1000.0000     Make bit high
                BRA     .BITHIGH        Bit is now high!

.BITLOW         ANDB    #%0111.1111     Make bit low
                NOP                     Compensate for BRA
                NOP

.BITHIGH        STAB    PIA_B
                BSR     .DELAY          Delay one bit time
                PULB                    Get bit counter back
                LSRA                    Shift next bit out
                DECB                    Decrement bit counter
                BNE     .LOOP           Do all 9 bits!

                LDAB    PIA_B           Transmit stopbit
                ORAB    #%1000.0000     Make bit high
                STAB    PIA_B
                SEI                     Re-enable interrupts
                BSR     .DELAY          Wait 1 final bit time
                PULB                    Restore affected registers
                PULA
                RTS

.DELAY          LDAB    #9              Get delay time factor
.DELAY_LOOP     DECB
                BNE     .DELAY_LOOP     Delay not over yet!
                NOP                     Compensate fractional part
                RTS

;----------------------------------------------------------------------
;  SNDBYTE
;  Send a byte in A as two hexadecimal characters to serial output
;----------------------------------------------------------------------

SNDBYTE         PSHA                    Save byte
                LSRA                    Isolate highest nibble
                LSRA
                LSRA
                LSRA
                ADDA    #'0'            Add ASCII offset to it
                CMPA    #'9'            Is it a digit?
                BLS     .DIGIT          Yes!
                ADDA    #7              Make it a letter

.DIGIT          JSR     SCHOUT          Send character out
                PULA                    Restore byte
                PSHA                     and save it again
                ANDA    #%0000.1111     Isolate lowest nibble
                ADDA    #'0'            Add ASCII offset to it
                CMPA    #'9'            Is it a digit?
                BLS     .DIGIT2         Yes!
                ADDA    #7              Make it a letter

.DIGIT2         JSR     SCHOUT          Send character
                PULA                    Restore affected register
                RTS

;----------------------------------------------------------------------
;  RDBYTE
;  Read a byte from the current address location from ROM
;----------------------------------------------------------------------

RDBYTE          LDAB    ADDRESS         Get MSBs of address
                ANDB    #%0000.1111
                ORAB    #%1100.0000     Keep Serial data high
                STAB    PIA_B            and reset' high
                LDAB    ADDRESS+1       Get LSBs of address
                STAB    PIA_A
                CLRB                    Select PIA A as outputs
                STAB    CON_A
                COMB
                STAB    PIA_A
                NOP                     Short delay
                NOP
                NOP
                NOP
                LDAB    PIA_B           Make reset' line low
                ANDB    #%1011.1111
                STAB    PIA_B
                CLRB                    Select PIA A as inputs
                STAB    PIA_A
                LDAB    #%0000.0100
                STAB    CON_A
                LDAA    PIA_A           Get data byte
                LDAB    PIA_B           Make reset' line high again
                ORAB    #%0100.0000
                STAB    PIA_B
                RTS

;----------------------------------------------------------------------
;  INCADDR
;  Increment ROM address pointer
;----------------------------------------------------------------------

INCADDR         INC     ADDRESS+1       Increment low byte
                BNE     .RTS            No carry into MSB
                INC     ADDRESS
.RTS            RTS

;----------------------------------------------------------------------
;  RESET
;  System RESET entry
;----------------------------------------------------------------------

RESET           LDS     #$007F          Reset stack pointer
                LDAA    #%0000.0100     Initialise PIA ports
                STAA    CON_A
                STAA    CON_B
                LDAA    #%1111.1111     Make all outputs high before
                STAA    PIA_A            they even become an output
                STAA    PIA_B
                CLRA
                STAA    CON_A
                STAA    CON_B
                STAA    PIA_A           PIA A is all inputs
                LDAA    #%1111.1111
                STAA    PIA_B           PIA B is all outputs
                LDAA    #%0000.0100     Select data registers again
                STAA    CON_A
                STAA    CON_B
                CLRA                    Reset address counter
                STAA    ADDRESS
                STAA    ADDRESS+1

                LDAA    #0              Start a relatively long delay
.LOOP1          LDAB    #0               before we really begin.
.LOOP2          DECB
                BNE     .LOOP2
                DECA
                BNE     .LOOP1

;----------------------------------------------------------------------
;  MAIN
;  Main program loop that reads all ROM addresses
;----------------------------------------------------------------------

MAIN            LDAA    #16             Send 16 bytes on this line
                STAA    COUNTER
                STAA    CHCKSUM
                LDAA    #':'            Send Intel file prefix
                JSR     SCHOUT
                LDAA    COUNTER         Send byte counter
                JSR     SNDBYTE
                LDAA    ADDRESS         Send 16 bit address
                JSR     SNDBYTE
                ADDA    CHCKSUM
                STAA    CHCKSUM
                LDAA    ADDRESS+1
                JSR     SNDBYTE
                ADDA    CHCKSUM
                STAA    CHCKSUM
                CLRA                    Send identifier
                JSR     SNDBYTE

.BYTE           JSR     RDBYTE          Read byte from 8048
                JSR     SNDBYTE          and send it to the master
                ADDA    CHCKSUM         Add it to the checksum
                STAA    CHCKSUM
                JSR     INCADDR         Increment address
                DEC     COUNTER         Decrement byte counter
                BNE     .BYTE           Not done yet!

                LDAA    CHCKSUM         Negate checksum
                COMA
                INCA
                JSR     SNDBYTE         And send it out
                LDAA    #CR             Send CR to output
                JSR     SCHOUT
                LDAA    ADDRESS         Done?
                ANDA    #%1111.0000
                BEQ     MAIN            Not yet!

                LDX     #ENDSTNG        Send end string
.LOOP           LDAA    0,X             Get character
                BEQ     .END            Done!
                JSR     SCHOUT          Send character out
                INX                     Point to next character
                BRA     .LOOP           Do next character
.END            JMP     .END            Keep doing nothing until reset

ENDSTNG         .AZ     /:00000001FF/,#CR

;----------------------------------------------------------------------
;  Vectors
;----------------------------------------------------------------------

                .OR     $FFF8
                .DA     RESET           IRQ
                .DA     RESET           SWI
                .DA     RESET           NMI
                .DA     RESET           Reset

                .LI     OFF

