home *** CD-ROM | disk | FTP | other *** search
/ Transactor / Transactor_24_1988_Transactor_Publishing.d64 / hexed.src < prev    next >
Text File  |  2023-02-26  |  21KB  |  693 lines

  1. *------------------------------*
  2. *  c64 hex  file editor        *
  3. *  (c) 1987 bob kodadek        *
  4. *      3164 surrey lane        *
  5. *      aston, pa  19014        *
  6. *------------------------------*
  7. *  merlin-128 macro-assembler  *
  8.  
  9. chrget   = $73               ;character get routine
  10. chrgot   = $79               ;get character again
  11. endadr   = $fb               ;pointer:highest address
  12. ramptr   = $fd               ;pointer:to ram
  13. temp     = $51               ;temporary usage
  14. flen     = $b7               ;filename length
  15. buffer   = $0100             ;work area
  16. buf      = $0200             ;system input buffer
  17. sa       = $0800             ;start of usable ram
  18. second   = $ff93             ;kernal equates
  19. tksa     = $ff96
  20. acptr    = $ffa5
  21. ciout    = $ffa8
  22. untlk    = $ffab
  23. unlsn    = $ffae
  24. listen   = $ffb1
  25. talk     = $ffb4
  26. readst   = $ffb7
  27. setlfs   = $ffba
  28. setnam   = $ffbd
  29. open     = $ffc0
  30. close    = $ffc3
  31. chkin    = $ffc6
  32. chkout   = $ffc9
  33. clrchn   = $ffcc
  34. chrin    = $ffcf
  35. chrout   = $ffd2
  36. stop     = $ffe1
  37. getin    = $ffe4
  38. plot     = $fff0
  39.  
  40. * note: labels beginning with "]" are variables
  41. *        and are used for backward branching only.
  42.  
  43.          org $c000
  44.  
  45. start    jsr setpnt          ;set ramptr
  46.          ldx #0              ;zero end address
  47.          stx endadr
  48.          sty endadr+1
  49.          stx sa              ;zero load address
  50.          stx sa+1
  51.          stx $0286           ;black for text color
  52. help     lda #15             ;color code for grey
  53.          sta $d020           ;set border color
  54.          sta $d021           ;set background color
  55.          lda #$93            ;clr screen
  56.          jsr chrout
  57.          ldx #2              ;locate cursor
  58.          ldy #12
  59.          clc
  60.          jsr plot            ;print menu screen
  61.          jsr primm
  62.          txt 'hex file editor'0d0d0d
  63.          txt '  menu    (c) 1987 bob kodadek'0d0d
  64.          txt 'e-edit'0d
  65.          txt 'l-list'0d
  66.          txt 'p-print'0d
  67.          txt 'd-directory'0d
  68.          txt 'r-read file'0d
  69.          txt 'w-write file'0d
  70.          txt 'x-disk command'0d
  71.          txt '#-dec to hex'0d
  72.          txt '$-hex to dec'0d
  73.          txt 'c-columns'0d
  74.          txt 'm-menu'0d
  75.          txt 'q-exit'00
  76.  
  77.          ldx #18             ;locate cursor
  78.          ldy #20
  79.          clc
  80.          jsr plot
  81.          jsr primm
  82.          txt 'load address:$'00
  83.          ldx sa
  84.          lda sa+1
  85.          jsr printhex        ;print load address
  86.          ldx #19             ;locate cursor
  87.          ldy #20
  88.          clc
  89.          jsr plot
  90.          jsr primm
  91.          txt 'object:$'00
  92.          ldx #<sa
  93.          lda #>sa
  94.          jsr printhex        ;print start address
  95.          jsr primm
  96.          txt '-$'00
  97.          ldx endadr
  98.          lda endadr+1
  99.          jsr printhex        ;print ending address
  100. * get command and execute
  101. getcom   jsr restore         ;restore channels
  102.          lda #$f0            ;chrget restored always
  103.          sta $82
  104.          lda #$ef
  105.          sta $83
  106.          lda #$0d
  107.          jsr chrout          ;print a cr
  108.          ldx #38             ;erase command line
  109.          ldy $d3
  110.          lda #$20
  111. ]erase   sta ($d1),y
  112.          iny
  113.          dex
  114.          bne ]erase
  115.          lda #'>'
  116.          jsr chrout          ;print command prompt
  117.          jsr input           ;get input
  118.          jsr chrget          ;read character
  119. ]loop    cmp table,y         ;compare to command table
  120.          beq docom           ;if found, do it
  121.          iny                 ;else, get more
  122.          cpy #$0c            ;tested all?
  123.          bne ]loop
  124.          jmp getcom          ;not legal command
  125. docom    tya                 ;get index into a
  126.          asl                 ;mulitply x 2
  127.          tax
  128.          lda adr+1,x         ;look up address
  129.          pha                 ;push it on stack
  130.          lda adr,x
  131.          pha
  132.          rts                 ;jump to command routine
  133.  
  134. table    asc 'rwlexdmqp#$c'
  135. adr      da  read-1          ;read file
  136.          da  write-1         ;write file
  137.          da  list-1          ;list to screen
  138.          da  edit-1          ;full screen editor
  139.          da  diskc-1         ;send disk command
  140.          da  direct-1        ;read directory
  141.          da  help-1          ;produce main menu
  142.          da  quit-1          ;return to basic
  143.          da  plist-1         ;list to printer
  144.          da  dechex-1        ;convert decimal to hex
  145.          da  hexdec-1        ;convert hex to decimal
  146.          da  change-1        ;select number of columns
  147.          hex 00
  148.  
  149. *** command routines ***
  150.  
  151. read = *
  152. * reads prg, seq, or usr file into ram
  153.          jsr fname           ;input filename
  154.          jsr setlog          ;set logical file
  155.          jsr setpnt          ;point to $0800
  156.          ldx #$03
  157.          jsr chkin           ;open input channel
  158.          ldy #$00
  159. ]loop    jsr chrin           ;input character
  160.          sta (ramptr),y  ;store in ram
  161.          jsr incpnt          ;increment ramptr
  162.          jsr readst          ;read status byte
  163.          sta erbyt           ;save it
  164.          cmp #64             ;test for eol
  165.          beq eof
  166.          lda erbyt           ;test for error
  167.          bne rderr           ;read error channel
  168.          jmp ]loop
  169. eof      sta (ramptr),y      ;store eof marker ($40)
  170.          tya                 ;y=0
  171. ]loop    iny
  172.          sta (ramptr),y  ;and a few zero bytes
  173.          cpy #3
  174.          bne ]loop
  175.          lda ramptr          ;move ramptr to endadr
  176.          sta endadr
  177.          lda ramptr+1
  178.          sta endadr+1
  179. rderr    jsr restore         ;clear channels
  180.          lda $ba             ;read error channel
  181.          jsr talk            ;device 8 talks
  182.          lda #$6f            ;from command channel
  183.          sta $b9
  184.          jsr tksa
  185.          lda #$0d
  186.          jsr chrout          ;print a cr
  187. ]get     jsr acptr           ;input serial byte
  188.          cmp #$0d            ;test for cr
  189.          beq enderr
  190.          jsr chrout          ;print the byte
  191.          jmp ]get
  192. enderr   jsr chrout          ;print the cr
  193.          jsr untlk           ;stop talking
  194.          jsr prrt            ;prompt "press return"
  195.          jsr chrin           ;wait for <return>
  196.          jmp help            ;display menu
  197. prrt     jsr primm
  198.          txt 'press return'00
  199.          rts
  200.  
  201. incpnt   inc ramptr          ;increment ram pointer
  202.          bne r1
  203.          inc ramptr+1
  204. r1        rts
  205.  
  206. write = *
  207. * writes a binary file in prg, seq, or usr format
  208.          jsr primm
  209.          txt 'type (p/s/u):'00
  210. ]get     jsr input           ;get user's file type
  211.          jsr chrget
  212.          sta ftyp            ;save it
  213.          jsr fname           ;get filename
  214.          ldx #$03
  215. ]loop    lda wr,x
  216.          sta buf,y           ;append file type
  217.          iny
  218.          dex
  219.          bpl ]loop
  220.          sty flen            ;set file length
  221.          jsr setlog          ;set up logical file
  222.          ldx #$03
  223.          jsr chkout          ;open output channel
  224.          jsr setpnt          ;point to start ($0800)
  225.          lda #$36            ;basic roms out
  226.          sta $01
  227. ]loop    lda ramptr          ;test for end
  228.          cmp endadr
  229.          bne w1
  230.          lda ramptr+1
  231.          cmp endadr+1
  232.          beq w2
  233. w1       ldy #$00
  234.          lda (ramptr),y  ;get ram byte
  235.          jsr ciout           ;output byte
  236.          jsr incpnt          ;increment ramptr
  237.          jmp ]loop
  238. w2       jmp rderr           ;read error channel
  239.  
  240. plist = *
  241. * lists to screen and printer
  242.          lda #4              ;set device to #4
  243.          sta $ba
  244.          jsr listen          ;printer listens
  245.          lda #$60
  246.          sta $b9
  247.          jsr second          ;secondary address 0
  248.          jsr primm           ;prompt "ready?"
  249.          dfb 13
  250.          txt 'ready? '00
  251.          jsr prrt            ;prompt "press return"
  252.          lda #$0d
  253.          jsr senchr          ;print a cr
  254. ]loop    jsr ckstop          ;test stop key
  255.          jsr $ffe4           ;wait for <return>
  256.          cmp #$0d
  257.          bne ]loop
  258.  
  259. list = *
  260. * list to screen and a listener, if present
  261.          jsr calcln          ;calculate line number
  262. ]loop    jsr ckstop          ;check stop key
  263.          jsr line            ;output line
  264. wait     lda 653             ;check shift key status
  265.          cmp #$01            ;freeze listing if active
  266.          beq wait
  267.          jsr incln           ;increment line number
  268.          jmp ]loop           ;do more
  269.  
  270. diskc = *
  271. * sends user command to drive
  272.          jsr primm           ;prompt user
  273.          txt 'command:'00
  274.          jsr input           ;get command
  275.          lda $ba             ;device 8 listens
  276.          jsr listen
  277.          lda #$6f            ;command channel
  278.          sta $b9
  279.          jsr second
  280.          ldy #$00
  281. ]loop    lda buf,y           ;read input buffer
  282.          beq d1
  283.          jsr ciout           ;send command string
  284.          iny
  285.          bne ]loop
  286. d1       jmp rderr           ;read error channel
  287.  
  288. quit = *
  289. * routine to exit back to basic
  290.          jsr primm           ;prompt
  291.          db  13
  292.          txt 'exit? (y/n)'00
  293. ]loop    jsr getin           ;getkey
  294.          cmp #'n'
  295.          beq q1
  296.          cmp #'y'
  297.          bne ]loop
  298.          jmp 64738           ;system reset
  299. q1       jmp help            ;or return to menu
  300.  
  301. direct = *
  302. * displays directory from current drive
  303.          lda #$01            ;setup filename "$"
  304.          ldx #<file
  305.          ldy #>file
  306.          jsr setnam
  307.          lda #$60
  308.          sta $b9
  309.          jsr $f3d5
  310.          lda $ba             ;device 8 talks
  311.          jsr talk
  312.          lda $b9
  313.          jsr tksa
  314.          lda #$00            ;clear status byte
  315.          sta $90
  316.          ldy #$03
  317. get      sty $b7
  318.          jsr acptr           ;get # blocks in file
  319.          sta $c3             ;save low byte
  320.          jsr acptr
  321.          sta $c4             ;and high byte
  322.          ldy $90             ;test status byte
  323.          bne dout            ;if not 0 then exit
  324.          ldy $b7
  325.          dey
  326.          bne get
  327.          ldx $c3
  328.          lda $c4
  329.          jsr $bdcd           ;print # blocks
  330.          lda #$20            ;print a space
  331.          jsr chrout
  332. ]loop    jsr acptr           ;read filename
  333.          ldx $90
  334.          bne dout            ;test status
  335.          cmp #$00            ;test for new line
  336.          beq newln
  337.          jsr chrout          ;print a character
  338.          jsr stop            ;test stop key
  339.          beq dout
  340.          jsr getin           ;get keypress
  341.          beq ]loop
  342.          cmp #$20            ;test for space bar
  343.          bne ]loop           ;freeze if pressed
  344. ]wait    jsr getin           ;wait for keypress
  345.          beq ]wait
  346.          bne ]loop           ;continue listing
  347. newln    lda #$0d            ;print cr
  348.          jsr chrout
  349.          ldy #$02
  350.          bne get             ;go do more
  351. dout     jsr $f642           ;restore channels
  352.          jmp getcom          ;next command
  353. file     asc '$'
  354.  
  355. edit = *
  356. * full screen editor routine
  357.          jsr calcln          ;calculate line number
  358. e1       lda #$0d            ;print a cr
  359.          jsr chrout
  360.          jsr line            ;print the line
  361.          ldy #34
  362. ]loop1   lda #157            ;reposition cursor
  363.          jsr chrout
  364.          dey
  365.          bpl ]loop1
  366.          lda #$00            ;input screen line
  367.          tay
  368. ]get     jsr $f15d
  369.          cmp #$0d
  370.          beq g1
  371.          sta buf,y           ;save in buffer
  372.          iny
  373.          bne ]get
  374. g1       ldx #$ff            ;point chrget to buf
  375.          ldy #$01
  376.          stx $7a
  377.          sty $7b
  378.          jsr calcln          ;read line number
  379.          jsr chrgot          ;get last char read
  380.          cmp #'-'            ;compare to "-"
  381.          bne nothex          ;exit if not
  382.          ldy #$00
  383. ]loop2   jsr edhex           ;editor hex asc to binary
  384.          sta buffer,y        ;save binary
  385.          jsr chrget          ;get next character
  386.          iny
  387.          cpy col             ;last column?
  388.          beq skip            ;then skip test
  389.          cmp #$20            ;else, must be a space
  390.          beq ]loop2
  391. nothex   lda #'?'            ;data error! exit
  392.          jsr chrout
  393.          jmp getcom
  394. * if no errors, now store the data in ram
  395. skip     ldy #0
  396. ]loop3   lda buffer,y        ;get binary value
  397.          sta (ramptr),y  ;store it in ram
  398.          iny
  399.          cpy col             ;done all columns?
  400.          bne ]loop3
  401.          jsr incln           ;increment line number
  402.          jmp e1
  403.  
  404. hexdec = *
  405. * outputs decimal from ascii hexadecimal input
  406.          jsr rdhex           ;get high byte
  407.          tay                 ;save it
  408.          jsr rdhex           ;get low byte
  409.          tax
  410.          tya
  411.          jsr $bdcd           ;print in decimal
  412.          jmp getcom          ;next command
  413.  
  414. dechex = *
  415. * outputs hexadecimal number from ascii decimal input
  416.          jsr ascint          ;asc to integer
  417.          lda #'$'            ;print "$"
  418.          jsr chrout
  419.          ldx $14             ;get low byte integer
  420.          lda $15             ;then high byte
  421.          jsr printhex        ;output number in hex
  422.          jmp getcom          ;get next command
  423.  
  424. change = *
  425. * user sets number of columns displayed (6-11)
  426.          jsr ascint          ;asc to integer
  427.          lda $14             ;get low byte
  428.          cmp #$06            ;<6 columns?
  429.          bcc ch1             ;then exit
  430.          cmp #12             ;=>12 columns?
  431.          bcs ch1             ;also exit
  432.          sta col             ;store if 6-11
  433. ch1      jmp getcom          ;get next command
  434.  
  435. **************** subroutines ****************
  436.  
  437. * c-64 print immediate routine allows imbedded string
  438. primm    pla                 ;remove return address
  439.          sta $22             ;save it as current pc
  440.          pla
  441.          sta $23
  442.          bne nxtchar         ;branch always
  443. pchr     jsr chrout
  444. nxtchar  ldy #0
  445.          inc $22             ;increment position
  446.          bne nc
  447.          inc $23
  448. nc       lda ($22),y         ;get text
  449.          bne pchr            ;print until #$00
  450.          lda $23             ;new return address
  451.          pha
  452.          lda $22
  453.          pha
  454.          rts                 ;get next instruction
  455.  
  456. * outputs line to current channel or listener
  457. line     jsr tstend          ;test for last line
  458.          ldy #$00
  459. ]loop    lda linum,y
  460.          beq l1
  461.          jsr senchr          ;send line number
  462.          iny
  463.          bne ]loop
  464. l1       lda #'-'
  465.          jsr senchr
  466.          ldy #0
  467.          sty cnt             ;zero counter
  468. ]jloop   lda (ramptr),y      ;read binary in ram
  469.          jsr prbyte          ;output as hex
  470.          lda #$20
  471.          jsr senchr          ;output space
  472.          inc cnt             ;increment counter
  473.          ldy cnt             ;compare to #columns
  474.          cpy col
  475.          bne ]jloop
  476.          lda #$0d
  477.          jsr senchr          ;output cr
  478.          rts
  479.  
  480. * output hex string from two byte integer
  481. printhex jsr prbyte          ;print a reg
  482.          txa                 ;print x reg
  483. prbyte   pha                 ;save a
  484.          lsr                 ;shift high nibble down
  485.          lsr
  486.          lsr
  487.          lsr
  488.          jsr phex            ;print it
  489.          pla                 ;pull original byte
  490. prnib    and #$0f            ;mask low nibble
  491. phex     ora #$30
  492.          cmp #$3a            ;decimal digit?
  493.          blt jco             ;branch if so
  494.          adc #6              ;add offset for hex
  495. jco      jsr senchr
  496.          rts
  497.  
  498. incln    ldx #$03            ;increments line number
  499. ]loop    inc linum,x
  500.          lda linum,x
  501.          cmp #$3a
  502.          bne in1
  503.          lda #$30
  504.          sta linum,x
  505.          dex
  506.          bpl ]loop
  507. in1      inc lnum
  508.          bne in2
  509.          inc lnum+1
  510. in2      lda ramptr          ;update ram pointer
  511.          adc col
  512.          sta ramptr
  513.          lda ramptr+1
  514.          adc #$00
  515.          sta ramptr+1
  516.          rts
  517.  
  518. tstend   sec                 ;test for highest address
  519.          lda ramptr          ;double-byte comparison
  520.          sbc endadr
  521.          sta temp
  522.          lda ramptr+1
  523.          sbc endadr+1
  524.          ora temp
  525.          bcs ts1             ;stop if greater
  526.          rts                 ;else ok
  527. ts1      pla
  528.          pla
  529.          jmp getcom
  530.  
  531. senchr   jsr ciout           ;send byte to listener
  532.          jsr chrout          ;send byte to screen
  533.          rts
  534.  
  535. setlog   lda flen            ;open logical file
  536.          ldx #$00
  537.          ldy #$02
  538.          jsr setnam
  539.          lda #$03
  540.          ldx #$08
  541.          ldy #$03
  542.          jsr setlfs
  543.          jsr open
  544.          rts
  545.  
  546. fname    jsr primm           ;prompt
  547.          txt 'file name:'00
  548.          jsr input           ;get file name
  549. ]loop    lda buf,y           ;get length
  550.          beq out
  551.          iny
  552.          bne ]loop
  553. out      sty flen            ;save it
  554.          ldy flen            ;test for no file name
  555.          bne f1
  556.          pla
  557.          pla
  558.          jmp help
  559. f1       rts
  560.  
  561. setpnt   ldx #<sa            ;reset ram pointer
  562.          ldy #>sa
  563.          stx ramptr
  564.          sty ramptr+1
  565.          rts
  566.  
  567. input    jsr $a560           ;get user input
  568.          stx $7a             ;point chrget
  569.          sty $7b
  570.          ldy #0
  571.          rts
  572.  
  573. * this routine translates an ascii line number into
  574. * the needed location in ram and sets the pointer
  575. * (ramptr) accordingly through incln.
  576. calcln   lda #$01            ;set integer line# to 1
  577.          sta lnum
  578.          lda #$00
  579.          sta lnum+1
  580.          jsr setpnt          ;set ramptr to $0800
  581.          lda #$30            ;set asc line# to 0001
  582.          ldy #$03
  583. ]loop    sta linum,y
  584.          dey
  585.          bpl ]loop
  586.          inc linum+3
  587.          jsr ascint          ;get integer
  588.          lda $14             ;greater than 0?
  589.          bne cal1            ;yes, continue
  590.          lda $15             ;else, exit
  591.          beq cal3
  592. cal1     lda lnum            ;get line number
  593.          cmp $14             ;test low byte
  594.          bne cal2            ;same?
  595.          lda lnum+1
  596.          cmp $15             ;test high byte
  597.          beq cal3            ;same?
  598. cal2     jsr incln           ;increment # & build string
  599.          jmp cal1
  600. cal3     rts
  601.  
  602. * this routine translates ascii hex into binary. the
  603. * first entry point modifies the chrget routine to
  604. * accept space characters for the screen editor.
  605. edhex    lda #$ea            ;modify chrget for edit
  606.          sta $82             ;store two nop instr.
  607.          sta $83
  608. rdhex    jsr chrget          ;get ascii character
  609.          jsr tsthex          ;test for hex
  610.          stx hex1            ;store it
  611.          jsr chrget          ;get next char.
  612.          jsr tsthex          ;test for hex
  613.          stx hex1+1          ;store it
  614.          lda hex1            ;get first value
  615.          asl                 ;multiply by 16
  616.          asl
  617.          asl
  618.          asl
  619.          clc                 ;add second value
  620.          adc hex1+1          ;now we have binary
  621. gothex   rts
  622.  
  623. tsthex   ldx #$00            ;test for hex 0-f
  624. ]loop    cmp hex,x           ;compare to table
  625.          beq gothex          ;x reg returns 0-15
  626.          inx                 ;else, do more
  627.          cpx #$10            ;any more left?
  628.          bne ]loop           ;not found
  629.          pla
  630.          pla                 ;pull 2 addresses
  631.          pla
  632.          pla
  633.          jmp nothex          ;report error!
  634.  
  635. * this routine converts ascii into a two-byte integer
  636. * as in the basic rom routine, but handles 0-65535.
  637. ascint   ldx #$00
  638.          stx $14
  639.          stx $15
  640. ]loop    jsr chrget          ;get asc character
  641.          bcs as1             ;set when not asc numeric
  642.          sbc #$2f            ;includes carry
  643.          sta $07             ;save remainder (0-9)
  644.          lda $15             ;build two byte integer
  645.          sta $22             ;temp area
  646.          lda $14
  647.          asl
  648.          rol $22
  649.          asl
  650.          rol $22
  651.          adc $14
  652.          sta $14
  653.          lda $22
  654.          adc $15
  655.          sta $15
  656.          asl $14
  657.          rol $15
  658.          lda $14
  659.          adc $07
  660.          sta $14
  661.          bcc ]loop
  662.          inc $15
  663.          bne ]loop
  664. as1      rts
  665.  
  666. ckstop   jsr stop            ;stop key pressed?
  667.          bne nostop          ;if not, then continue
  668.          pla                 ;else remove return
  669.          pla                 ;address from stack
  670.          jmp getcom          ;and jump to command
  671. nostop   rts                 ;routine.
  672.  
  673. restore  jsr unlsn           ;unlisten device
  674.          jsr clrchn          ;clear channels
  675.          lda $b8             ;get file number
  676.          jsr close           ;close logical file
  677.          lda #$08            ;set device to 8
  678.          sta $ba
  679.          lda #$37            ;basic rom's in
  680.          sta $01
  681.          rts
  682.  
  683. hex1     ds  2               ;storage
  684. lnum     ds  2               ;holds line number
  685. cnt      ds  1               ;counter
  686. erbyt    ds  1               ;holds status byte
  687. col      db  8               ;holds # columns
  688. linum    asc '0000'00        ;asc line #
  689. hex      asc '0123456789'
  690.          asc 'abcdef'
  691. wr       asc 'w,'            ;file direction
  692. ftyp     asc 'p,'            ;file type
  693.