home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / JSAGE / ZSUS / PROGPACK / HLOAD.LBR / HLOAD.AQM / HLOAD.ASM
Assembly Source File  |  2000-06-30  |  7KB  |  333 lines

  1. ; HEXLOAD - Load a Hex file anywhere in memory
  2. ;
  3.     TITLE    'HEXLOAD - Load a hex file, (1-12/17/82)'
  4.     PAGE    60
  5. ;
  6. ; This program will load a hex file at its org address, 
  7. ; wherever this is in memory, as long as it does not overlay
  8. ; HLOAD itself.  Usage is:
  9. ;
  10. ;    HLOAD file-name
  11. ;
  12. ; If no file type is given, HEX is assumed.  If the file is
  13. ; not found, the file (with the HEX type) is looked for on the
  14. ; A drive.  After the file is loaded, control is returned to
  15. ; CP/M without a warm-start.
  16. ;
  17. ; HLOAD differs from the LOAD command of CP/M in that it does
  18. ; not create a .COM file, and it does not expect the .HEX file
  19. ; to be ORGed at 100H.  It is useful for loading memory
  20. ; outside the normal CP/M boundaries.  For example, I use it
  21. ; to load an extended CBIOS, by including the command
  22. ;
  23. ;    HLOAD CBIOSOVR
  24. ;
  25. ; as my auto-start-up command.
  26. ;
  27. ;
  28. ; This program requires MAC and MACRO.LIB to assemble.
  29. ;
  30. ;
  31. ; Author:
  32. ;    Don McClimans
  33. ;    Inti Electronics Corporation
  34. ;    41 Washburn Pk.
  35. ;    Rochester, NY 14620
  36. ;    716-271-6280
  37. ;
  38. ; Send all comments, bugs, etc. to the author at the above
  39. ; address.
  40. ;
  41. ; This program is in the public domain, and may be freely
  42. ; copied or used for any purpose.
  43. ;
  44. ;
  45. ; Revision List (reverse order):
  46. ;  1 12/17/82 Original design by DPM
  47. ;
  48. ; The standard Intel Hex format consists of lines like this:
  49. ;
  50. ;    :nnaaaa00xxxxxxxx...xxkk
  51. ;
  52. ; where nn is the number of data bytes on the line, aaaa is 
  53. ; the address to load at, xx are series of data bytes, and kk
  54. ; is a checksum.  The checksum (kk) is formed by subtracting
  55. ; each byte of the line (including nn and each half of the 
  56. ; address aaaa), in 8-bit arithmetic, from zero (thus the sum
  57. ; including kk should be zero).  The final line of a file 
  58. ; should contain a byte count of zero - the address field in 
  59. ; this line gives the starting address, which is ignored by
  60. ; this program.
  61. ;
  62. ;
  63. ; Load Macro file
  64. ;
  65.     MACLIB    MACRO        ;DR Standard MACRO library
  66. ;
  67. ;
  68. ; Start of Program
  69. ;
  70.     ORG    100H
  71. START:
  72.     LXI    H,0        ;Get system stack pointer
  73.     DAD    SP
  74.     LXI    SP,STACK    ;Set to use local stack
  75.     PUSH    H        ;Preserve system stack pointer
  76. ;
  77. ; Try to open the file
  78. ;
  79.     DISKIO    OPEN,FCB    ;Open file name as given
  80.     CPI    0FFH        ;Was open good
  81.     JNZ    FILEOPEN    ;Yes - go process
  82.                 ;No - try same name with HEX
  83.     MOVE    HEXTYPE,FCBTYPE,3    ;file type
  84.     DISKIO    OPEN,FCB    ;Try to open it again
  85.     CPI    0FFH        ;Was open good?
  86.     JNZ    FILEOPEN    ;Yes - go process
  87.                 ;No - try same name, with HEX
  88.     MVI    A,1        ; on the A drive
  89.     STA    FCBDRIVE
  90.     DISKIO    OPEN,FCB    ;Try to open it again
  91.     CPI    0FFH        ;Was open good?
  92.     JNZ    FILEOPEN    ;Yes - go process
  93. ;
  94.     PRINT    'File Not Found'    ;Give errror message
  95.     PRINT
  96.     JMP    RETCPM
  97. ;
  98. ; Entry here when file is open
  99. ;
  100. FILEOPEN:
  101.     MVI    A,0        ;Initialize variables
  102.     STA    LINENUM
  103.     STA    LINENUM+1
  104.     LXI    H,BUFR+128
  105.     SHLD    BUFPTR
  106. ;
  107. ; Entry here to start reading a new line
  108. ; Skip leading CR, LF, and blanks
  109. ; Quit on EOF (Ctrl-Z)
  110. ;
  111. NEWLINE:
  112.     XRA    A        ;Zero the checksum
  113.     STA    CHECKSUM
  114.     LXI    H,LINENUM
  115.     INR    M        ;Increment line number
  116.     JNC    SKIPTOCOLON
  117.     INX    H
  118.     INR    M
  119. SKIPTOCOLON:
  120.     CALL    READBYTE
  121.     CPI    CR
  122.     JZ    SKIPTOCOLON
  123.     CPI    LF
  124.     JZ    NEWLINE
  125.     CPI    ' '
  126.     JZ    SKIPTOCOLON
  127.     CPI    EOFCH        ;Is it ctrl-Z?
  128.     JZ    CLOSEFILE    ;Yes - quit
  129.     CPI    STARTCH        ;Is it a colon?
  130.     JZ    GETLENGTH    ;Yes - go process
  131. BADNUM:
  132.     PRINT    'HLOAD: Error in line '
  133.     DECOUT    LINENUM
  134.     PRINT
  135.     JMP    CLOSEFILE
  136. ;
  137. ; Entry here after colon read - get the number of data bytes
  138. ;
  139. GETLENGTH:
  140.     CALL    READBYTE
  141.     STA    NUMBUFF
  142.     CALL    READBYTE
  143.     STA    NUMBUFF+1
  144.     XRA    A        ;zero the third byte
  145.     STA    NUMBUFF+2
  146.     HEXIN    NUMBUFF
  147.     JC    BADNUM        ;Quit if errror
  148.     CALL    ADDTOCHECK
  149.     STA    NUMBYTES
  150.     ANA    A        ;Was number of bytes zero?
  151.     JZ    CLOSEFILE    ;Yes - we are done
  152. ;
  153. ; Length has been read - get the address to load at.
  154. ;
  155.     CALL    READBYTE
  156.     STA    NUMBUFF
  157.     CALL    READBYTE
  158.     STA    NUMBUFF+1
  159.     CALL    READBYTE
  160.     STA    NUMBUFF+2
  161.     CALL    READBYTE
  162.     STA    NUMBUFF+3
  163.     XRA    A        ;zero the fifth byte
  164.     STA    NUMBUFF+4
  165.     HEXIN    NUMBUFF
  166.     JC    BADNUM        ;Quit if errror
  167.     SHLD    LOADADR
  168.     CALL    ADDTOCHECK
  169.     MOV    A,H
  170.     CALL    ADDTOCHECK
  171. ;
  172. ; Address has ben read.  Read the next two zero's, and throw
  173. ; away.
  174. ;
  175.     CALL    READBYTE
  176.     CALL    READBYTE
  177. ;
  178. ; Read all the data bytes and store them.
  179. ;
  180. ; Entry here to read the next byte and store it in the correct
  181. ; memory location.  The NUMBYTES counter is decremented till
  182. ; it reaches zero
  183. ;
  184. NEXTBYTE:
  185.     CALL    READBYTE
  186.     STA    NUMBUFF
  187.     CALL    READBYTE
  188.     STA    NUMBUFF+1
  189.     XRA    A        ;zero the third byte
  190.     STA    NUMBUFF+2
  191.     HEXIN    NUMBUFF
  192.     JC    BADNUM        ;Quit if errror
  193.     PUSH    PSW        ;Preserve registers
  194.     LHLD    LOADADR        ;Get load address
  195.     MOV    A,H        ;And check that it is not
  196.     ANA    A        ;in HLOAD memory image
  197.     JZ    LOADOK        ;High byte 00, so below HLOAD
  198.     LXI    D,HLOADEND    ;Subtract last address of HLOAD
  199.     DAD    D
  200.     JC    LOADOK
  201. ;
  202.     PRINT    'HLOAD: Load address overlays HLOAD in line '
  203.     DECOUT    LINENUM
  204.     PRINT
  205.     POP    PSW
  206.     JMP    CLOSEFILE
  207. ;
  208. LOADOK:
  209.     POP    PSW        ;Restore byte
  210.     LHLD    LOADADR
  211.     MOV    M,A        ;Store into memory
  212.     INX    H
  213.     SHLD    LOADADR
  214.     CALL    ADDTOCHECK
  215.     LXI    H,NUMBYTES    ;Decrement Counter
  216.     DCR    M
  217.     JNZ    NEXTBYTE    ;Loop till Zero
  218. ;
  219. ; Entry here when all bytes read
  220. ; Get the checksum and add it in - then check for zero
  221. ; checksum
  222. ;
  223.     CALL    READBYTE
  224.     STA    NUMBUFF
  225.     CALL    READBYTE
  226.     STA    NUMBUFF+1
  227.     XRA    A        ;Zero the third byte
  228.     STA    NUMBUFF+2
  229.     HEXIN    NUMBUFF
  230.     JC    BADNUM
  231.     CALL    ADDTOCHECK
  232.     LDA    CHECKSUM
  233.     ANA    A
  234.     JZ    NEWLINE
  235. ;
  236. ; Entry here with checksum error
  237. ;
  238.     PRINT    'HLOAD: Checksum error in line '
  239.     DECOUT    LINENUM
  240.     PRINT            ;Fall into CLOSEFILE
  241. ;
  242. ; Entry here to close the file and return to CP/M
  243. ;
  244. CLOSEFILE:
  245.     DISKIO    CLOSE,FCB
  246. ;
  247. ; Entry here to return to CPM
  248. ;
  249. RETCPM:
  250.     POP    H        ;Get system stack
  251.     SPHL
  252.     RET
  253. ;
  254. ;
  255. ; SUBROUTINES:
  256. ;
  257. ; READBYTE: Read a byte from the file.  Return Ctrl-Z for all
  258. ; EOF's.  Byte returned in A register - Uses all regs
  259. ;
  260. READBYTE:
  261.     LHLD    BUFPTR        ;Get buffer pointer
  262.     MOV    A,H        ;See if at 100H (past end)
  263.     DCR    A
  264.     JNZ    NOREAD        ;No - so get next byte
  265. ;                ;Yes - so read a sector
  266.     DISKIO    READ,FCB
  267.     ORA    A        ;Read errors?
  268.     JZ    READOK        ;No - go process sector
  269.     DCR    A        ;Was A 1 meaning EOF?
  270.     JNZ    RDERR        ;No - go process error
  271.     MVI    A,EOFCH        ;Yes - return eof character
  272.     RET            ;and return
  273. ;
  274. RDERR:
  275.     PRINT    'Read error at line'
  276.     DECOUT    LINENUM        ;Print error message
  277.     PRINT
  278.     MVI    A,EOFCH        ;and return eof character
  279.     RET
  280. ;
  281. READOK:
  282.     LXI    H,BUFR        ;Start at beginning of buffer
  283. ;
  284. NOREAD:
  285.     MOV    A,M        ;Get next byte
  286.     INX    H        ;Increment pointer
  287.     SHLD    BUFPTR
  288.     RET
  289. ;
  290. ; ADDTOCHECH - Add A register to CHECKSUM
  291. ;        All registers preserved
  292. ;
  293. ADDTOCHECK:
  294.     PUSH    H
  295.     PUSH    PSW
  296.     LXI    H,CHECKSUM
  297.     ADD    M
  298.     MOV    M,A
  299.     POP    PSW
  300.     POP    H
  301.     RET
  302. ;
  303. ;
  304. ; Equates
  305. ;
  306. STARTCH:    EQU    ':'    ;Colon
  307. EOFCH:        EQU    26    ;Ctrl-Z
  308. FCB:        EQU    05CH    ;Address of default FCB
  309. FCBTYPE:     EQU    065H    ;Address of type in FCB
  310. FCBDRIVE:    EQU    05CH    ;Address of drive number in FCB
  311. BUFR:        EQU    080H    ;Standard "DMA" buffer
  312. BDOS:        EQU    5    ;BDOS call location
  313. ;
  314. ; Constants
  315. ;
  316. HEXTYPE:    DB    'HEX'    ;Default file type
  317. ;
  318. ; Variables
  319. ;
  320.         DS    80H    ;Local stack
  321. STACK:        EQU    $
  322. LINENUM:    DS    2    ;Number of lines read
  323. NUMBUFF:    DS    5    ;Buffer for number conversion
  324. NUMBYTES:    DS    1    ;Number of data bytes on line
  325. LOADADR:    DS    2    ;Address to load next byte at
  326. CHECKSUM:    DS    1    ;Working checksum
  327. BUFPTR:        DS    2    ;Pointer into BUFR
  328. HLOADEND:    EQU    1-$
  329. ;
  330. ; End of program
  331. ;
  332.     END    START
  333.