home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / forth / pfe-0.000 / pfe-0 / pfe-0.9.13 / demo / 6809sim / readme.09 < prev   
Encoding:
Text File  |  1994-06-19  |  12.0 KB  |  314 lines

  1. The 6809 assembler/simulator/debugger
  2. -------------------------------------
  3.  
  4. Written by:
  5.  
  6. L.C. Benschop
  7. Eindhoven
  8. The Netherlands.
  9.  
  10. Introduction
  11. ------------
  12.  
  13. 6809sim contains a Forth assembler for the Motorola 6809 processor and an
  14. instruction-level simulator with full-screen memory editing and interactive
  15. debugging. There is also a disassembler.
  16.  
  17. 6809sim started as a hobby project under F-PC in 1989. I was fascinated by
  18. the elegant and powerful instruction set of the 6809, but I did not have
  19. one. So I decided to write one myself. Forth was _the_ language of choice by
  20. then, so I write it in Forth. I started with the assembler. I wanted to have
  21. an assembler with normal syntax instead of a typical Forth assembler. In
  22. fact I wanted an assembler like that of F-PC. I succeeded in writing one.
  23. Further I wanted an interactive processor simulator with memory editing, a
  24. register display etc. I had seen something similar with a 6502. I got to
  25. have a 6809 version.
  26.  
  27. In 1993 I ported the thing to PFE. At the same time I wrote a C-version of
  28. the simulator with no fancy interactive debugging, but it ran 40 times
  29. faster. I kept the simulators compatible, so they could run each other's
  30. programs. That helped me tremendously, both in finding bugs in any of the
  31. simulators and in debugging 6809 code. Most 6809 programs that I write or
  32. adapt today, are not written in Forth assembler but with a conventional
  33. assembler. One such program is a primitive port of E-Forth. It runs on the
  34. simulator, but it is assembled on a normal assembler. I know I *should* have
  35. a metacompiled Forth for the thing but I never finished writing one.
  36.  
  37. 6809sim.4th is mostly ANSI compatible. It runs on pfe-0.95 and hopefully
  38. on later versions too. PFE can be obtained via ftp from 
  39.        roxi.rz.fht-mannheim.de pub/unix/languages/forth
  40.  
  41. To load 6809sim, start pfe and type
  42.       include 6809sim.4th
  43. After some messages of redefined words, the simulator is ready for use
  44. You start it by entering 
  45.       simulate
  46. You quit it by typing Q. The ? key gives you some help.
  47.  
  48. The assembler
  49. _____________
  50.  
  51.  
  52. The 6809 assembler is a Forth assembler whose syntax is as close to normal 
  53. assembler as one can get. As the instructions are parsed by the normal
  54. Forth parser, there are some differences. The most important ones are:
  55. - There must be spaces between all components of an instruction.
  56.   Therefore on writes 
  57.     LDA # $3E    instead of LDA #$3E
  58.     PSHS A, X, Y instead of PSHS A,X,Y
  59.     STA 15 ,X    instead of STA 15,X
  60.  
  61. - The constant 0 is required in register indirect addressing.
  62.     STB 0 ,S     instead of STB ,S
  63.  
  64. - No # in the instructions ANDCC ORCC and CWAI
  65.     ANDCC $FE    instead of ANDCC #$FE
  66.  
  67. - Indirect mode is signalled by the word [] after the address
  68.   instead of the address inside brackets. For example
  69.     ADDA 1 ,U [] instead of ADDA [1,U]
  70.  
  71. - Expressions are postfix.
  72.     ADCB DATA1 14 + instead of ADCB DATA1+14
  73.    
  74. - Labels must be preceded by the word LABEL and only backward 
  75.   references to labels are allowed. 
  76.  
  77.   LABEL SUBR
  78.       ADDA 4 ,X
  79.       STA 5 ,X
  80.       RTS
  81.   
  82.   LABEL MAIN 
  83.       JSR SUBR
  84.  
  85.   instead of
  86.   SUBR ADDA 4,X
  87.        STA 5,X
  88.        RTS
  89.   MAIN JSR SUBR
  90.   
  91.   This way one writes the program bottom-up with the main routine last.
  92.   If one must have a forward reference (e.g. to jump to the main
  93.   routine) one can write a JMP -1 at the start and later
  94.   patch the address using the word V! Like this.
  95.   
  96.   LABEL START
  97.   JMP -1
  98.   .....
  99.   LABEL MAIN
  100.  
  101.   MAIN START 1 + V! \ Patch the address of main in the jump.
  102.  
  103. - The Forth control structures BEGIN .. UNTIL BEGIN .. WHILE .. REPEAT
  104.   IF .. THEN and IF .. ELSE .. THEN are allowed, like in Forth assemblers.
  105.   These are translated to conditional and unconditional branch instructions.
  106.  
  107.   Allowable conditions are:
  108.    0= 0<> 0>= 0< < >= > <= U< U>= U> U<= VC (overflow clear) and VS
  109.    (overflow set). Note that the conditions generate a branch whose
  110.    mnemonic is the opposite of the condition, e.g. VC IF generates
  111.    a BVS instruction.   
  112.   
  113.   The following Forth assembler code
  114.  
  115.     0>= if
  116.       leax 1 ,x
  117.     else
  118.       leax -1 ,x
  119.     then
  120.      
  121.    replaces the more conventional
  122.  
  123.     bmi lab1
  124.     leax 1,x
  125.     bra lab2
  126.    lab1    leax -1,x
  127.    lab2 
  128.  
  129. - The words , and C, enter constant bytes and words, ALLOT reserves space
  130.        $1244 ,    instead of  FDB $1244
  131.        $55 C,     instead of  FCB $55
  132.        25 ALLOT   instead of  RMB 25
  133.        $100 ORG   instead of  ORG $100
  134.  
  135. An assembler source file must start with the word ASSEMBLE and
  136. end with the word ENDASM It is assembled from the Forth commandline with
  137.       include filename
  138. or it can be assembled from the interactive simulator. The assembled
  139. binary result resides inside the 64 memory space of the virtual 6809.
  140.  
  141. Two example assembler files are included, mirror.asm and bin2dec.asm.
  142.  
  143.  
  144. Loading, saving and disassembling of binary files.
  145. __________________________________________________
  146.  
  147.  
  148. Once a source file is assembled, the binary result can be saved to disk.
  149. The word VSAVE expects an address and a length on the stack. The command
  150.      $1000 $100 vsave foo
  151. saves 256 bytes starting at address $1000 into the file foo.
  152.  
  153. We can assemble the file mirror.asm and save the binary code by.
  154. include mirror.asm
  155. 0 $19 vsave mirror.bin
  156.  
  157. We could also add the VSAVE command to the assembler source file.
  158.  
  159. The word VLOAD loads a binary file into the memory of the virtual 6809.
  160. It expects an address on the stack. The command
  161.   0 vload mirror.bin
  162. loads back the binary result of mirror.asm, without assembling first,
  163.  
  164. The word DISAS disassembles a section of memory.  It expects two addresses
  165. on the stack. Suppose the file mirror.asm was assembled. The command
  166.  
  167.     0 $18 disas 
  168. shows the following listing.
  169.  
  170. LDY # $0040          \ 0000 10 8E 00 40 
  171. LDB ,Y+              \ 0004 E6 A0 
  172. STB $80              \ 0006 D7 80 
  173. LDX # $0081          \ 0008 8E 00 81 
  174. ABX                  \ 000B 3A 
  175. TSTB                 \ 000C 5D 
  176. BEQ $0016            \ 000D 27 07 
  177. LDA ,Y+              \ 000F A6 A0 
  178. STA ,-X              \ 0011 A7 82 
  179. DECB                 \ 0013 5A 
  180. BNE $000F            \ 0014 26 F9 
  181. JMP $FFFF            \ 0016 7E FF FF 
  182.  
  183.  
  184.  
  185. The interactive simulator
  186. _________________________
  187.  
  188. The Forth command SIMULATE starts the interactive simulator. This should
  189. show a HEX/ASCII display of the memory of the 6809 with a display of the
  190. processor registers at the bottom.
  191.  Q leaves the simulator.
  192.  ? shows a help screen.
  193.   
  194. One can use the cursor keys and home/end/pgup/pgdn keys to move the cursor
  195. throughout the 64K memory space, of which 256 bytes are visible at a time.
  196. If these keys fail, one can use alternatively the Wordstar keys control-S
  197. control-D control-E and control-X as cursor keys, control-R and control-C
  198. for page up and down and control-A and control-F for begin and end of line.
  199. The space key moves also to the next location.
  200.  
  201. The keys 0-9 and the letters A-F change the HEX value at the cursor
  202. location. The ASCII display changes accordingly, but it cannot be edited
  203. directly. 
  204.  
  205. The following one-letter commands manipulate the assembler file:
  206.  N (asks for a name) sets the name of the assembler file.
  207.  Z invoke the editor on the assembler file.
  208.  Y assembles the assembler file.
  209.  
  210. As an example, select the file mirror.asm (N command) and assemble it
  211. (Y command). Then move the cursor to the address $0040 and enter a
  212. string preceded by a count byte, like in the display below.
  213.  
  214.        0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F 0123456789ABCDEF
  215. 0000  10 8E 00 40 E6 A0 D7 80 8E 00 81 3A 5D 27 07 A6 ...@f W ...:]'.&
  216. 0010  A0 A7 82 5A 26 F9 7E FF FF 00 00 00 00 00 00 00  '.Z&y~  .......
  217. 0020  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  218. 0030  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  219. 0040  05 46 4F 52 54 48 00 00 00 00 00 00 00 00 00 00 .FORTH..........
  220. 0050  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  221. 0060  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  222. 0070  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  223. 0080  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  224. 0090  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  225. 00A0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  226. 00B0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  227. 00C0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  228. 00D0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  229. 00E0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  230. 00F0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  231. CC=00000000  A=$00 B=$00 DP=$00 X=$0000 Y=$0000 U=$0000 S=$0000 
  232.    EFHINZVC PC=$0000 LDY # $0040                                 
  233.  
  234. The following commands execute instructions.
  235.  T executes the instruction at the program counter location (that is
  236.    displayed at the bottom).
  237.  G asks for a breakpoint address and executes instructions until the
  238.    breakpoint is encountered.
  239.  S sets a breakpoint just after the instruction at the program
  240.    counter location and starts running until that breakpoint. This
  241.    way one can execute a whole subroutine instead of just a JSR
  242.    instruction.
  243.                                                                                 1HH101H
  244. Try to single-step the mirror program using the T command and watch the
  245. registers and the memory.
  246.  
  247. The following commands are also useful in debugging.
  248.   R asks for a register, type A,B,X,Y,U,S,P (for PC) C (for CC) or
  249.     D (for DP) and asks for a value. Change a particular register.
  250.   P sets the cursor location to the program counter location.
  251.   X sets the program counter location to the cursor location.
  252.   U sets the program counter to the next sequential instruction,
  253.     without executing.
  254.  
  255. A session of an X command followed by several U commands shows the
  256. disassembled instructions of a program fragment in the bottom of the
  257. display.
  258.  
  259. Remove the string at address $80 by typing zeros over it. 
  260.  
  261. Now reset the program counter to location 0 (either by moving the
  262. cursor to location 0 and typing X, or using the R P command) and
  263. run the program using the G command. Give FFFF for breakpoint. 
  264.  
  265. Try to run the program with different strings at address $40.
  266.  
  267. Move the cursor to address 0 and type X. Then type several
  268. U's in a row. Watch the disassembled instructions.
  269.  
  270. The following commands are for file loading and saving.
  271.   L (asks for address and name) loads a binary file like VLOAD
  272.   W (asks for address, length and name) saves a binary file 
  273.     like VSAVE.
  274.  
  275. The simulator executes all normal 6809 instructions, but the following
  276. instructions have special meanings.
  277.    SWI2 prints the character in the B register to the screen.
  278.    SWI3 reads a key from the keyboard (if available) into the B
  279.         register. Resets carry flag if key available, sets carry
  280.         if no key available.
  281.    SYNC Exits the simulator with a beep.
  282.  
  283. One can always interrupt the simulator with control-U. One can restart the
  284. simulator by retyping SIMULATE. Note that a program run interrupted
  285. by ^U cannot be properly continued, as an instruction may be interrupted
  286. halfway. Normally one would leave the simulator with Q.
  287.  
  288. The program bin2dec.asm is slightly more complicated than mirror.asm
  289. It is assembled at address $100 so you must move one page down and you
  290. must set the program counter to $100. It converts numbers to decimal and
  291. prints them using SWI2. Run the program, single step it, step over
  292. subroutine calls using the S command. Look how the BCD number is generated
  293. at address $11B, watch the decrementing counters at addresses $120 and
  294. $121, see the stack pointer decrement and increment with subroutine calls
  295. and returns, and see the actual stack at $1FF.    
  296.  
  297.  
  298. Related programs
  299. ________________
  300.  
  301.  
  302. A C version of the simulator, a conventional 6809 assembler and some example
  303. programs (a tiny BASIC and E-Forth) were posted to alt.sources in May 94.
  304. These are archived at some sites, for example
  305.          wuarchive.wustl.edu usenet/alt.sources/articles
  306.           the files 10405.Z 10406.Z and 10407.Z
  307. They should be obtainable from nic.funet.fi too, but this site has a
  308. different archive organization.
  309.  
  310. The example programs run also under the interactive simulator in Forth.
  311.  
  312.  
  313.  
  314.