Advertisement
antiquekid3

Brainfuck Interpreter - SWTPC 6800 ASM

Apr 28th, 2012
2,212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Brainfuck Interpreter
  2. ;antiquekid3 - 3/28/2012
  3. ;
  4. ;Enter your BF program and press enter to execute. RAM will first be cleared,
  5. ;then program will start. Program finishes with "\n\rdone!" printed to screen.
  6. ;If you make a mistake during the entry of your program, DEL ($7F), which is
  7. ;mapped to the backspace key for many people, will allow you to correct the
  8. ;program. Invalid characters are not entered, saving valuable RAM space. A true
  9. ;backspace character ($08, or ^H) will also operate as a DEL.
  10.  
  11. ;A handy subroutine can be found at the bottom for printing null-terminated
  12. ;strings.
  13.  
  14. ;Typically, another stack is used for storing the return addresses when jumping
  15. ;to subroutines, since the main stack is used to enter the program, and then is
  16. ;used for the "tape" memory. Since the 6800 lacks a Y index register, one must
  17. ;make due with what one has.
  18.  
  19. ;One thing I've noticed is that if I switch around the direction of the tape,
  20. ;such that the starting end is higher in RAM (and I change the > and < commands
  21. ;accordingly), the program runs much slower. Any clue as to why that would be?
  22. ;I have a feeling it could be a result from direct vs. extended addressing,
  23. ;but I'm not entirely sure.
  24.  
  25. OUTCH       EQU $E1D1
  26. INCH        EQU $E1AC
  27. CONTRL  EQU $E0E3
  28. RAMSTART    EQU $0000       ;start of RAM (tape for BF program)
  29. ARRAY       EQU $5000       ;start of BF code array
  30. ARREND  EQU $5BFF       ;end of BF code array
  31. VARS        EQU $5C00       ;variables
  32. SCRATCH EQU $5D00       ;start of code
  33. RAMEND  EQU $5FFF       ;end of RAM
  34. START       EQU $A048       ;for SWTBUG's 'G' command
  35.  
  36. ;Memory Map:
  37. ;RAMSTART to ARRAY = data
  38. ;ARRAY to ARREND = BF code
  39. ;VARS to SCRATCH = variables
  40. ;SCRATCH to $BFFF = ASM code
  41.  
  42. ;SP = data index
  43. ;X = code index
  44.  
  45.             ORG START
  46.             FDB SCRATCH ;set $A048 = #SCRATCH (start of program)
  47.  
  48.             ORG VARS
  49. Greet       FCB $0A,$0D
  50.             FCC "Enter code and press return:"
  51. LF          FCB $0A,$0D,$00
  52. Clear       FCC "Clearing RAM..."
  53.             FCB $00
  54. Done        FCC "done!"
  55.             FCB $00
  56. bs          FCB $20,$08,$08,$20,$08,$00     ;DEL key sequence
  57. bs1     FCB $20,$08,$00                     ;^H key sequence
  58. invalid FCB $08,$3F,$08,$00
  59. Address RMB 2           ;used to store current SP
  60. Brace       RMB 1           ;used to count up to 255 (brace depth)
  61.             RMB 8           ;used as return address stack for JSR
  62. RtnAdd  RMB 1
  63.  
  64.             ORG SCRATCH
  65.             LDX #Greet
  66.             LDS #RtnAdd
  67.             JSR PRINT       ;print greeting message
  68.            
  69.             LDS #ARREND
  70. input:  JSR INCH
  71.             CMPA    #$0D        ;return
  72.             BEQ run
  73.            
  74.             TAB
  75.             ANDB    #%11111101
  76.             CMPB    #','
  77.             BEQ valid
  78.             CMPA    #'+'
  79.             BEQ valid
  80.             CMPA    #'-'
  81.             BEQ valid
  82.             CMPB    #'<'
  83.             BEQ valid
  84.             CMPA    #'['
  85.             BEQ valid
  86.             CMPA    #']'
  87.             BEQ valid
  88.            
  89.             CMPA    #$7F        ;"delete" (backspace key)
  90.             BNE next
  91. del:        TSX
  92.             DEX
  93.             CPX #ARREND
  94.             BEQ input
  95.             LDX #bs
  96.             JSR PRINT
  97.             PULA
  98.             BRA input
  99.            
  100. fwd:        LDAA    #$20
  101.             JSR OUTCH
  102. toInput:    BRA input
  103.            
  104. next:       CMPA    #8          ;"backspace" (^H)
  105.             BNE inv
  106.             TSX
  107.             DEX
  108.             CPX #ARREND
  109.             BEQ fwd
  110.             PULA
  111.             LDX #bs1
  112.             JSR PRINT
  113.             BRA input
  114.            
  115. inv:        LDX #invalid
  116.             JSR PRINT
  117.             BRA toInput
  118. valid:  PSHA
  119.             BRA toInput
  120.            
  121. run:        PSHA                ;mark EOF as $0D (return)
  122.             LDX #LF
  123.             JSR PRINT
  124.  
  125. ;zero out data array
  126.             LDX #Clear
  127.             JSR PRINT
  128.             LDX #ARRAY
  129. zero:       CLR 0,X
  130.             DEX
  131.             CPX #$FFFF
  132.             BNE zero
  133.  
  134. run1:       LDX #Done
  135.             JSR PRINT
  136.             LDX #LF
  137.             JSR PRINT
  138.             LDX #ARREND
  139.             LDS #RAMSTART
  140.             DES             ;since PUL first increments SP, decrease SP beforehand
  141.             INX
  142.             CLR Brace
  143.  
  144. loop:       DEX
  145.             LDAA    0,X
  146.             CMPA    #'>'
  147.             BNE next2
  148.             INS
  149.             BRA loop
  150.  
  151. next2:  CMPA    #'<'
  152.             BNE next3
  153.             DES
  154.             BRA loop
  155.            
  156. next3:  CMPA    #'+'
  157.             BNE next4
  158.             PULA
  159.             INCA
  160.             PSHA
  161.             BRA loop
  162.            
  163. next4:  CMPA    #'-'
  164.             BNE next5
  165.             PULA
  166.             DECA
  167.             PSHA
  168.             BRA loop
  169.            
  170. next5:  CMPA    #','
  171.             BNE next6
  172.             PULA
  173.             STS Address
  174.             LDS #RtnAdd
  175.             JSR INCH
  176.             LDS Address
  177.             PSHA
  178.             BRA loop
  179.            
  180. next6:  CMPA    #'.'
  181.             BNE leftb
  182.             PULA
  183.             PSHA
  184.             STS Address
  185.             LDS #RtnAdd
  186.             JSR OUTCH
  187.             LDS Address
  188.             BRA loop
  189.            
  190. leftb:  CMPA    #'['
  191.             BNE rightb
  192.             CLR Brace
  193.             INC Brace
  194.             PULA
  195.             PSHA
  196.             TSTA
  197.             BNE loop
  198. find1:  DEX
  199.             LDAA    0,X
  200.             CMPA    #']'
  201.             BNE notrb1
  202.             DEC Brace
  203.             BEQ loop
  204.             BRA find1
  205. notrb1: CMPA    #'['
  206.             BNE find1
  207.             INC Brace
  208.             BRA find1
  209.            
  210. rightb: CMPA    #']'
  211.             BNE stop
  212.             CLR Brace
  213.             INC Brace
  214.             PULA
  215.             PSHA
  216.             TSTA
  217.             BEQ loop
  218. find2:  INX
  219.             LDAA    0,X
  220.             CMPA    #'['
  221.             BNE notlb2
  222.             DEC Brace
  223.             BEQ toLoop
  224.             BRA find2
  225. notlb2: CMPA    #']'
  226.             BNE find2
  227.             INC Brace
  228.             BRA find2
  229.        
  230. stop:       LDS #RtnAdd
  231.             LDX #LF
  232.             JSR PRINT
  233.             LDX #Done
  234.             JSR PRINT
  235.             LDX #SCRATCH
  236.             STX START
  237.             JMP CONTRL
  238. toLoop: JMP loop
  239.            
  240.            
  241. ;Subroutine to print a null-terminated string
  242. ;A = (changed)
  243. ;B = (not changed)
  244. ;X = starting address of string (changed)
  245. ;S = (not changed)
  246. PRINT:  LDAA    0,X
  247.             BEQ END
  248.             JSR OUTCH
  249.             INX
  250.             BRA PRINT
  251. END:        RTS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement