Advertisement
babusha

Untitled

Nov 7th, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; bfsim 2.0
  2. ;
  3. ; robert@robos.org
  4. ; http://robos.org
  5. ;
  6. ; BrainFuck system for pic16f84a
  7. ;
  8. ; Original version by Robert Цstling, August 2003.
  9. ; Rewritten February 2004 - bug fixes. Lots of bug fixes.
  10. ;
  11. ; Note: Keep all the tables far away from any 256 words boundaries.
  12. ;
  13. ; Instructions:
  14. ;
  15. ; +  00  Increase value at pointer by one.
  16. ; -  01  Decrease value at pointer by one.
  17. ; >  02  Increase pointer by one.
  18. ; <  03  Decrease pointer by one.
  19. ; [  04  Jump to next ]
  20. ; ]  05  Jump to previous [ unless value at pointer is zero.
  21. ; .  06  Output value at pointer to portb.
  22. ; ,  07  Input value from porta (bits 1 to 3) and store to pointer.
  23. ;
  24. ; Program memory    - 128 instructions (64 bytes) of EEPROM.
  25. ; RAM memory        - 32 bytes (0x20 to 0x3f).
  26. ; Stack         - 16 bytes (0x40 to 0x4f).
  27. ;
  28. ; I/O:
  29. ;
  30. ; porta0        - "enter" key.
  31. ; porta1 - porta3   - Input value (0 to 7).
  32. ; portb         - Output (LEDs).
  33. ;
  34. ; 10000001 on the LEDs means: Input command.
  35. ; 11000011 on the LEDs means: Input data to user program.
  36.  
  37.  
  38.     list    p=PIC16F84A
  39.     include "p16f84a.inc"
  40.  
  41.     cblock 0x0c
  42.     iptr,ptr,sptr,cptr,nest,tmp
  43.     endc
  44.  
  45.     page
  46.     __config _CP_OFF & _LP_OSC & _PWRTE_ON & _WDT_OFF
  47.  
  48.     org 0
  49.  
  50. reset:
  51.     bcf intcon,gie      ; disable interrupts
  52.     call    initialize      ; initialize vars, mem, i/o
  53.  
  54. main_loop:
  55.     movlw   0x81
  56.     movwf   portb           ; output 10000001 to portb
  57.     call    input
  58.     clrf    portb
  59.     movfw   tmp         ; w = command
  60.     andlw   0x03
  61.  
  62.     addwf   pcl         ; execute command from keyboard:
  63.     goto    cmd_00          ; input opcode
  64.     goto    cmd_01          ; reset program pointer
  65.     goto    cmd_02          ; "backspace"
  66.     goto    cmd_03          ; run program
  67.  
  68. ; input opcode
  69. cmd_00:
  70.     call    input           ; input byte to tmp
  71.     movfw   cptr
  72.     incf    cptr
  73.     movwf   eeadr           ; eeadr = cptr++
  74.     call    nibble_write        ; write to eeprom
  75.     goto    main_loop
  76.  
  77. ; reset program pointer
  78. cmd_01:
  79.     clrf    cptr
  80.     goto    main_loop
  81.  
  82. ; "backspace"
  83. cmd_02:
  84.     decf    cptr
  85.     goto    main_loop
  86.  
  87. ; run program
  88. cmd_03:
  89.     clrf    iptr
  90. cmd_03_loop:
  91.     call    execute
  92.     incf    iptr
  93.     goto    cmd_03_loop
  94.  
  95. ; execute one instruction
  96. execute:
  97.     movfw   iptr
  98.     movwf   eeadr           ; read instruction from iptr
  99.     call    nibble_read
  100.  
  101.     addwf   pcl         ; execute instruction
  102.     goto    instr_00        ; +
  103.     goto    instr_01        ; -
  104.     goto    instr_02        ; >
  105.     goto    instr_03        ; <
  106.     goto    instr_04        ; [
  107.     goto    instr_05        ; ]
  108.     goto    instr_06        ; .
  109.     goto    instr_07        ; ,
  110.  
  111. ; +
  112. instr_00:
  113.     movfw   ptr
  114.     addlw   0x20
  115.     movwf   fsr         ; point to current memory location
  116.     incf    indf            ; increase it
  117.     return
  118.  
  119. ; -
  120. instr_01:
  121.     movfw   ptr
  122.     addlw   0x20
  123.     movwf   fsr         ; point to current memory location
  124.     decf    indf            ; decrease it
  125.     return
  126.  
  127. ; >
  128. instr_02:
  129.     incf    ptr
  130.     movlw   0x1f
  131.     andwf   ptr         ; ptr = (ptr + 1) mod 0x20
  132.     return
  133.  
  134. ; <
  135. instr_03:
  136.     decf    ptr
  137.     movlw   0x1f
  138.     andwf   ptr         ; ptr = (ptr - 1) mod 0x20
  139.     return
  140.  
  141. ; [
  142. instr_04:
  143.     incf    iptr            ; iptr++
  144.     decf    sptr
  145.     movfw   sptr
  146.     movwf   fsr
  147.     movfw   iptr
  148.     movwf   indf            ; push iptr to stack
  149.     movlw   0x01
  150.     movwf   nest            ; nest = 1
  151.     decf    iptr            ; now let's find the next ] instruction
  152. instr_04_find:
  153.     incf    iptr
  154.     movfw   iptr
  155.     movwf   eeadr
  156.     call    nibble_read     ; read nest instruction
  157.     sublw   0x05            ; is it "]" ?
  158.     bz  instr_04_end
  159.     sublw   0x01            ; is it "[" ?
  160.     btfsc   status,z
  161.     incf    nest            ; yes, increase nest
  162.     goto    instr_04_find  
  163. instr_04_end:
  164.     decf    nest
  165.     bnz instr_04_find       ; if --nest != 0, continue
  166.     decf    iptr            ; iptr--
  167.     return              ; otherwise return to normal execution
  168.  
  169. ; ]
  170. instr_05:
  171.     movfw   ptr
  172.     addlw   0x20
  173.     movwf   fsr
  174.     movfw   indf            ; get value at current memory location
  175.     sublw   0x00
  176.     bnz instr_05_loop       ; is it 0?
  177.     incf    sptr
  178.     return              ; yes, drop value on stack and continue.
  179. instr_05_loop:
  180.     movfw   sptr
  181.     movwf   fsr
  182.     movfw   indf
  183.     movwf   iptr            ; iptr = top of stack, don't change sptr
  184.     decf    iptr            ; iptr--
  185.     return              ; continue execution
  186.  
  187. ; .
  188. instr_06:
  189.     movfw   ptr
  190.     addlw   0x20
  191.     movwf   fsr
  192.     movfw   indf
  193.     movwf   portb           ; output value on current memory location to portb
  194.     call    input_wait      ; wait for "enter" key
  195.     return
  196.  
  197. ; ,
  198. instr_07:
  199.     movlw   0xc3
  200.     movwf   portb           ; output 11000011 to portb
  201.     movfw   ptr
  202.     addlw   0x20
  203.     movwf   fsr         ; point to current memory location
  204.     call    input           ; read byte from keyboard
  205.     movfw   tmp
  206.     movwf   indf            ; store to memory
  207.     clrf    portb           ; clear portb
  208.     return
  209.  
  210. input:
  211.     call    input_wait      ; wait for "enter" key
  212.     movfw   porta
  213.     andlw   0x0e
  214.     movwf   tmp
  215.     bcf status,c
  216.     rrf tmp         ; tmp = (byte >> 1) mod 8
  217.     return
  218.  
  219. input_wait:
  220.     btfss   porta,0
  221.     goto    input_wait      ; wait for "enter" key
  222.     clrf    tmp
  223. input_wait1:
  224.     incf    tmp
  225.     btfss   tmp,3
  226.     goto    input_wait1     ; short delay
  227. input_wait2:
  228.     btfsc   porta,0
  229.     goto    input_wait2     ; wait for "enter" key to release
  230.     return
  231.  
  232. ; read nibble addressed by eeadr to w
  233. nibble_read:
  234.     btfsc   eeadr,0
  235.     goto    nibble_read_odd     ; check if we're reading an odd nibble
  236.     bcf status,c
  237.     rrf eeadr           ; eeadr = eeadr / 2
  238.     call    eeprom_read
  239.     movfw   eedata          ; read byte to w
  240.     andlw   0x0f            ; mask out high 4 bits
  241.     return
  242. nibble_read_odd:
  243.     bcf status,c
  244.     rrf eeadr           ; eeadr = eeadr / 2
  245.     call    eeprom_read
  246.     swapf   eedata          ; swap high and low nibble
  247.     movfw   eedata
  248.     andlw   0x0f            ; mask out high 4 bits
  249.     return
  250.  
  251. ; read byte from eeadr to eedata
  252. eeprom_read:
  253.     bsf status,rp0
  254.     bsf eecon1,rd
  255.     bcf status,rp0
  256.     return
  257.  
  258. ; write the nibble in tmp to the address in eeadr
  259. nibble_write:
  260.     bcf status,c
  261.     rrf eeadr           ; eeadr = eeadr / 2
  262.     bc  nibble_write_odd    ; check if we're writing to an odd nibble
  263.     call    eeprom_read
  264.     movfw   eedata          ; read byte
  265.     andlw   0xf0            ; mask out low nibble
  266.     addwf   tmp,w           ; fill it with our data
  267.     goto    nibble_write_done
  268. nibble_write_odd:
  269.     call    eeprom_read
  270.     movfw   eedata          ; read byte
  271.     andlw   0x0f            ; mask out high nibble
  272.     swapf   tmp
  273.     addwf   tmp,w           ; fill it with out data
  274. nibble_write_done:
  275.     movwf   eedata
  276.  
  277. ; write the byte on eedata to the address in eeadr
  278. eeprom_write:
  279.     bsf status,rp0
  280.     bsf eecon1,wren
  281.     movlw   0x55
  282.     movwf   eecon2
  283.     movlw   0xaa
  284.     movwf   eecon2
  285.     bsf eecon1,wr
  286.     bcf status,rp0
  287.     return
  288.  
  289. initialize:
  290.     clrf    porta
  291.     clrf    portb
  292.     bsf status,rp0
  293.     movlw   0x1f
  294.     movwf   trisa           ; set porta to input mode
  295.     clrf    trisb           ; set portb to output mode
  296.     bcf status,rp0
  297.  
  298.     movlw   0x01
  299.     movwf   portb
  300. demo_loop:
  301.     bcf status,c
  302.     rlf portb           ; portb = portb << 1
  303.     btfsc   status,c        ; did we shift out the bit?
  304.     incf    portb           ; yes, set portb to 1 again
  305.     clrf    tmp
  306. demo_delay:
  307.     decfsz  tmp
  308.     goto    demo_delay      ; 10ms delay
  309.     btfss   porta,0
  310.     goto    demo_loop       ; if "enter" key is not pressed, loop
  311.     call    input_wait      ; wait for its release
  312.  
  313.     clrf    portb
  314.     clrf    iptr
  315.     clrf    ptr
  316.     movlw   0x50
  317.     movwf   sptr
  318.     clrf    cptr
  319.     movlw   0x20
  320.     movwf   fsr
  321.  
  322. clear_ram:
  323.     clrf    indf
  324.     incf    fsr
  325.     btfss   fsr,6
  326.     goto    clear_ram
  327.  
  328.     return
  329.  
  330.     end
  331.  
  332.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement