home *** CD-ROM | disk | FTP | other *** search
- The 6809 assembler/simulator/debugger
- -------------------------------------
-
- Written by:
-
- L.C. Benschop
- Eindhoven
- The Netherlands.
-
- Introduction
- ------------
-
- 6809sim contains a Forth assembler for the Motorola 6809 processor and an
- instruction-level simulator with full-screen memory editing and interactive
- debugging. There is also a disassembler.
-
- 6809sim started as a hobby project under F-PC in 1989. I was fascinated by
- the elegant and powerful instruction set of the 6809, but I did not have
- one. So I decided to write one myself. Forth was _the_ language of choice by
- then, so I write it in Forth. I started with the assembler. I wanted to have
- an assembler with normal syntax instead of a typical Forth assembler. In
- fact I wanted an assembler like that of F-PC. I succeeded in writing one.
- Further I wanted an interactive processor simulator with memory editing, a
- register display etc. I had seen something similar with a 6502. I got to
- have a 6809 version.
-
- In 1993 I ported the thing to PFE. At the same time I wrote a C-version of
- the simulator with no fancy interactive debugging, but it ran 40 times
- faster. I kept the simulators compatible, so they could run each other's
- programs. That helped me tremendously, both in finding bugs in any of the
- simulators and in debugging 6809 code. Most 6809 programs that I write or
- adapt today, are not written in Forth assembler but with a conventional
- assembler. One such program is a primitive port of E-Forth. It runs on the
- simulator, but it is assembled on a normal assembler. I know I *should* have
- a metacompiled Forth for the thing but I never finished writing one.
-
- 6809sim.4th is mostly ANSI compatible. It runs on pfe-0.95 and hopefully
- on later versions too. PFE can be obtained via ftp from
- roxi.rz.fht-mannheim.de pub/unix/languages/forth
-
- To load 6809sim, start pfe and type
- include 6809sim.4th
- After some messages of redefined words, the simulator is ready for use
- You start it by entering
- simulate
- You quit it by typing Q. The ? key gives you some help.
-
- The assembler
- _____________
-
-
- The 6809 assembler is a Forth assembler whose syntax is as close to normal
- assembler as one can get. As the instructions are parsed by the normal
- Forth parser, there are some differences. The most important ones are:
- - There must be spaces between all components of an instruction.
- Therefore on writes
- LDA # $3E instead of LDA #$3E
- PSHS A, X, Y instead of PSHS A,X,Y
- STA 15 ,X instead of STA 15,X
-
- - The constant 0 is required in register indirect addressing.
- STB 0 ,S instead of STB ,S
-
- - No # in the instructions ANDCC ORCC and CWAI
- ANDCC $FE instead of ANDCC #$FE
-
- - Indirect mode is signalled by the word [] after the address
- instead of the address inside brackets. For example
- ADDA 1 ,U [] instead of ADDA [1,U]
-
- - Expressions are postfix.
- ADCB DATA1 14 + instead of ADCB DATA1+14
-
- - Labels must be preceded by the word LABEL and only backward
- references to labels are allowed.
-
- LABEL SUBR
- ADDA 4 ,X
- STA 5 ,X
- RTS
-
- LABEL MAIN
- JSR SUBR
-
- instead of
- SUBR ADDA 4,X
- STA 5,X
- RTS
- MAIN JSR SUBR
-
- This way one writes the program bottom-up with the main routine last.
- If one must have a forward reference (e.g. to jump to the main
- routine) one can write a JMP -1 at the start and later
- patch the address using the word V! Like this.
-
- LABEL START
- JMP -1
- .....
- LABEL MAIN
-
- MAIN START 1 + V! \ Patch the address of main in the jump.
-
- - The Forth control structures BEGIN .. UNTIL BEGIN .. WHILE .. REPEAT
- IF .. THEN and IF .. ELSE .. THEN are allowed, like in Forth assemblers.
- These are translated to conditional and unconditional branch instructions.
-
- Allowable conditions are:
- 0= 0<> 0>= 0< < >= > <= U< U>= U> U<= VC (overflow clear) and VS
- (overflow set). Note that the conditions generate a branch whose
- mnemonic is the opposite of the condition, e.g. VC IF generates
- a BVS instruction.
-
- The following Forth assembler code
-
- 0>= if
- leax 1 ,x
- else
- leax -1 ,x
- then
-
- replaces the more conventional
-
- bmi lab1
- leax 1,x
- bra lab2
- lab1 leax -1,x
- lab2
-
- - The words , and C, enter constant bytes and words, ALLOT reserves space
- $1244 , instead of FDB $1244
- $55 C, instead of FCB $55
- 25 ALLOT instead of RMB 25
- $100 ORG instead of ORG $100
-
- An assembler source file must start with the word ASSEMBLE and
- end with the word ENDASM It is assembled from the Forth commandline with
- include filename
- or it can be assembled from the interactive simulator. The assembled
- binary result resides inside the 64 memory space of the virtual 6809.
-
- Two example assembler files are included, mirror.asm and bin2dec.asm.
-
-
- Loading, saving and disassembling of binary files.
- __________________________________________________
-
-
- Once a source file is assembled, the binary result can be saved to disk.
- The word VSAVE expects an address and a length on the stack. The command
- $1000 $100 vsave foo
- saves 256 bytes starting at address $1000 into the file foo.
-
- We can assemble the file mirror.asm and save the binary code by.
- include mirror.asm
- 0 $19 vsave mirror.bin
-
- We could also add the VSAVE command to the assembler source file.
-
- The word VLOAD loads a binary file into the memory of the virtual 6809.
- It expects an address on the stack. The command
- 0 vload mirror.bin
- loads back the binary result of mirror.asm, without assembling first,
-
- The word DISAS disassembles a section of memory. It expects two addresses
- on the stack. Suppose the file mirror.asm was assembled. The command
-
- 0 $18 disas
- shows the following listing.
-
- LDY # $0040 \ 0000 10 8E 00 40
- LDB ,Y+ \ 0004 E6 A0
- STB $80 \ 0006 D7 80
- LDX # $0081 \ 0008 8E 00 81
- ABX \ 000B 3A
- TSTB \ 000C 5D
- BEQ $0016 \ 000D 27 07
- LDA ,Y+ \ 000F A6 A0
- STA ,-X \ 0011 A7 82
- DECB \ 0013 5A
- BNE $000F \ 0014 26 F9
- JMP $FFFF \ 0016 7E FF FF
-
-
-
- The interactive simulator
- _________________________
-
- The Forth command SIMULATE starts the interactive simulator. This should
- show a HEX/ASCII display of the memory of the 6809 with a display of the
- processor registers at the bottom.
- Q leaves the simulator.
- ? shows a help screen.
-
- One can use the cursor keys and home/end/pgup/pgdn keys to move the cursor
- throughout the 64K memory space, of which 256 bytes are visible at a time.
- If these keys fail, one can use alternatively the Wordstar keys control-S
- control-D control-E and control-X as cursor keys, control-R and control-C
- for page up and down and control-A and control-F for begin and end of line.
- The space key moves also to the next location.
-
- The keys 0-9 and the letters A-F change the HEX value at the cursor
- location. The ASCII display changes accordingly, but it cannot be edited
- directly.
-
- The following one-letter commands manipulate the assembler file:
- N (asks for a name) sets the name of the assembler file.
- Z invoke the editor on the assembler file.
- Y assembles the assembler file.
-
- As an example, select the file mirror.asm (N command) and assemble it
- (Y command). Then move the cursor to the address $0040 and enter a
- string preceded by a count byte, like in the display below.
-
- 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
- 0000 10 8E 00 40 E6 A0 D7 80 8E 00 81 3A 5D 27 07 A6 ...@f W ...:]'.&
- 0010 A0 A7 82 5A 26 F9 7E FF FF 00 00 00 00 00 00 00 '.Z&y~ .......
- 0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
- 0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
- 0040 05 46 4F 52 54 48 00 00 00 00 00 00 00 00 00 00 .FORTH..........
- 0050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
- 0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
- 0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
- 0080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
- 0090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
- 00A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
- 00B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
- 00C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
- 00D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
- 00E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
- 00F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
- CC=00000000 A=$00 B=$00 DP=$00 X=$0000 Y=$0000 U=$0000 S=$0000
- EFHINZVC PC=$0000 LDY # $0040
-
- The following commands execute instructions.
- T executes the instruction at the program counter location (that is
- displayed at the bottom).
- G asks for a breakpoint address and executes instructions until the
- breakpoint is encountered.
- S sets a breakpoint just after the instruction at the program
- counter location and starts running until that breakpoint. This
- way one can execute a whole subroutine instead of just a JSR
- instruction.
- 1HH101H
- Try to single-step the mirror program using the T command and watch the
- registers and the memory.
-
- The following commands are also useful in debugging.
- R asks for a register, type A,B,X,Y,U,S,P (for PC) C (for CC) or
- D (for DP) and asks for a value. Change a particular register.
- P sets the cursor location to the program counter location.
- X sets the program counter location to the cursor location.
- U sets the program counter to the next sequential instruction,
- without executing.
-
- A session of an X command followed by several U commands shows the
- disassembled instructions of a program fragment in the bottom of the
- display.
-
- Remove the string at address $80 by typing zeros over it.
-
- Now reset the program counter to location 0 (either by moving the
- cursor to location 0 and typing X, or using the R P command) and
- run the program using the G command. Give FFFF for breakpoint.
-
- Try to run the program with different strings at address $40.
-
- Move the cursor to address 0 and type X. Then type several
- U's in a row. Watch the disassembled instructions.
-
- The following commands are for file loading and saving.
- L (asks for address and name) loads a binary file like VLOAD
- W (asks for address, length and name) saves a binary file
- like VSAVE.
-
- The simulator executes all normal 6809 instructions, but the following
- instructions have special meanings.
- SWI2 prints the character in the B register to the screen.
- SWI3 reads a key from the keyboard (if available) into the B
- register. Resets carry flag if key available, sets carry
- if no key available.
- SYNC Exits the simulator with a beep.
-
- One can always interrupt the simulator with control-U. One can restart the
- simulator by retyping SIMULATE. Note that a program run interrupted
- by ^U cannot be properly continued, as an instruction may be interrupted
- halfway. Normally one would leave the simulator with Q.
-
- The program bin2dec.asm is slightly more complicated than mirror.asm
- It is assembled at address $100 so you must move one page down and you
- must set the program counter to $100. It converts numbers to decimal and
- prints them using SWI2. Run the program, single step it, step over
- subroutine calls using the S command. Look how the BCD number is generated
- at address $11B, watch the decrementing counters at addresses $120 and
- $121, see the stack pointer decrement and increment with subroutine calls
- and returns, and see the actual stack at $1FF.
-
-
- Related programs
- ________________
-
-
- A C version of the simulator, a conventional 6809 assembler and some example
- programs (a tiny BASIC and E-Forth) were posted to alt.sources in May 94.
- These are archived at some sites, for example
- wuarchive.wustl.edu usenet/alt.sources/articles
- the files 10405.Z 10406.Z and 10407.Z
- They should be obtainable from nic.funet.fi too, but this site has a
- different archive organization.
-
- The example programs run also under the interactive simulator in Forth.
-
-
-
-