Interactive 8051 assembler and ICP for P2D2H Busy Bee support micro - work in progress
Peter Jakacki
Posts: 10,193
The new P2D2 was going to have a nice simple PIC chip in an 8-pin MSOP as a support micro for the P2 to monitor brownouts and DTR and serial commands. The other day though I found another micro in 20-pin QFN that is only 3x3mm and costs around 30 cents. The extra I/O will be useful for monitoring various aspects of the P2D2 but also because this is a Silabs micro it can be programmed in-circuit very easily either via a 2-wire C2 interface or via its serial bootloader. Being 8051 based there are plenty of tools available for it but since I have the C2 software working in TAQOZ I thought it would be far more flexible if I wrote an assembler to suit this micro that could run on the P2 itself. Although I have still a ways to go in finishing it off I have made some good progress in a little under two days on and off since last week.
It is a one-pass forward referencing assembler that is also suitable for typing or pasting standard format assembly code into the P2 or loading from the a file on the SD and via the C2 debug interface it can also program the micro in-circuit. It produces a listing as it processes each line but forward references will not be listed correctly although they are resolved through a linked list once the symbol is defined. I might make it handle a 2nd pass for a final listing if it is being loaded from a file.
I don't expect this assembler to be used much but I will do something similar with an assembler for the P2 where you can even test out assembly code interactively.
While the assembler is written in TAQOZ Forth it is also more correct to say that the assembler is written with Forth in that the Forth compiler is enhanced and changes its behavior to suit.
A simple example of defining the JNC operation in TAQOZ where the action of assembling is deferred until the end of the line while the destination parameter is resolved and available, ready to be used.
Here's a terminal capture of the assembler itself being compiled and then assembling assembler source that has been attached to the end of the source code for the assembler as this lets me check it out quickly.
The assembler includes a whole heap of special function register definitions but at present only takes around 2kB of code space, with another 1.6kB for the dictionary. Don't look too closely as there are a ton of errors in this listing which also includes a debug column but I expect to have most of this working this week as I find time.
It is a one-pass forward referencing assembler that is also suitable for typing or pasting standard format assembly code into the P2 or loading from the a file on the SD and via the C2 debug interface it can also program the micro in-circuit. It produces a listing as it processes each line but forward references will not be listed correctly although they are resolved through a linked list once the symbol is defined. I might make it handle a 2nd pass for a final listing if it is being loaded from a file.
I don't expect this assembler to be used much but I will do something similar with an assembler for the P2 where you can even test out assembly code interactively.
While the assembler is written in TAQOZ Forth it is also more correct to say that the assembler is written with Forth in that the Forth compiler is enhanced and changes its behavior to suit.
A simple example of defining the JNC operation in TAQOZ where the action of assembling is deferred until the end of the line while the destination parameter is resolved and available, ready to be used.
pub JNC DEFER: $50 RELOP ;Whereas the MOV operation tests different addressing modes but all MOV A,Rx or MOV A,@Rx operations are detected and handled with just 4 bytes
$E0 AROPand AROP is only about 14 bytes long etc.
Here's a terminal capture of the assembler itself being compiled and then assembling assembler source that has been attached to the end of the source code for the assembler as this lets me check it out quickly.
The assembler includes a whole heap of special function register definitions but at present only takes around 2kB of code space, with another 1.6kB for the dictionary. Don't look too closely as there are a ton of errors in this listing which also includes a debug column but I expect to have most of this working this week as I find time.
TAQOZ# TAQOZ Parallax P2 .:.:--TAQOZ--:.:. V1.1--RAM 181110-2300 321 322 lines and 2,140 bytes compiled, with 0 errors in 3,091ms ok TAQOZ# ok TAQOZ# ASM51 *** TAQOZ ASM51 ASSEMBLER *** 0000 0000: 00000000 0001 0000: AD PID: equ $AD 00000000 (1) $00AD 0002 0000: B6 RID: equ $B6 00000000 (1) $00B6 0003 0000: 0C RXD0: equ 12 00000000 (1) $000C 0004 0000: A5 K1: equ $A5 00000000 (1) $00A5 0005 0000: 40 VARS: equ $40 00000000 (1) $0040 0006 0000: 00000000 0007 0000: org $180 00000000 (1) $0180 0008 0180: D4 MAIN: DA 00000000 0009 0181: 90 D2 04 MOV DPTR,#1234 00000010 (1) $04D2 0010 0184: 54 22 ANL A,#$34 10000000 (1) $0022 0011 0186: 54 34 ANL C,$34 00001000 (1) $0034 0012 0188: 5F ANL A,R7 10010000 (1) $000F 0013 0189: 24 EF ADD A,#$34 10000000 (1) $0022 0014 018B: 24 EF ADD A,VARS 10000000 (1) $0040 0015 018D: 85 EF 64 MOV A,#$100 10000000 (1) $0064 0016 0190: 02 80 01 LJMP MAIN 00000000 (1) $0180 0017 0193: 40 EB JC MAIN 00000000 (1) $0180 0018 0195: 20 0C E8 JB RXD0,MAIN 00000000 (2) $0180 $000C 0019 0198: 10 80 0A JNB $A5,MAIN 00000000 (2) $00A5 $0180 0020 019B: 10 A5 E2 JNB K1,MAIN 00000000 (2) $0180 $00A5 0021 019E: E4 CLR A 10000000 0022 019F: C3 CLR C 00001000 0023 01A0: C2 0C CLR RXD0 00000000 (1) $000C 0024 01A2: D3 SETB C 00001000 0025 01A3: 20 80 9F JB $45,MAIN 00000000 (2) $0045 $0180 0026 01A6: 12 A7 01 LCALL FWD 00000000 (1) $01A7 0027 01A9: 73 JMP @A+DPTR 00000000 (1) $01AA 0028 01AA: 50 D4 JNC MAIN 00000000 (1) $0180 0029 01AC: 70 D2 JNZ MAIN 00000000 (1) $0180 0030 01AE: 60 D0 JZ MAIN 00000000 (1) $0180 0031 01B0: 40 CE JC MAIN 00000000 (1) $0180 0032 01B2: 80 CC SJMP MAIN 00000000 (1) $0180 0033 01B4: 02 80 01 LJMP MAIN 00000000 (1) $0180 0034 01B7: D2 0C SETB RXD0 00000000 (1) $000C 0035 01B9: C3 FWD: CLR C 00001000 0036 01BA: EF MOV A,R7 10010000 (1) $000F 0037 01BB: F8 MOV R0,A 01100000 (1) $0008 0038 01BC: F6 MOV @R0,A 01100000 (1) $0006 0039 01BD: F7 MOV @R1,A 01100000 (1) $0007 0040 01BE: E6 MOV A,@R0 10010000 (1) $0006 0041 01BF: E7 MOV A,@R1 10010000 (1) $0007 0042 01C0: E8 MOV A,R0 10010000 (1) $0008 0043 01C1: EF MOV A,R7 10010000 (1) $000F 0044 01C2: 46 ORL A,@R0 10010000 (1) $0006 0045 01C3: 47 ORL A,@R1 10010000 (1) $0007 0046 01C4: 48 ORL A,R0 10010000 (1) $0008 0047 01C5: 4F ORL A,R7 10010000 (1) $000F 0048 01C6: 96 SUBB A,@R0 10010000 (1) $0006 0049 01C7: 97 SUBB A,@R1 10010000 (1) $0007 0050 01C8: 98 SUBB A,R0 10010000 (1) $0008 0051 01C9: 9F SUBB A,R7 10010000 (1) $000F 0052 01CA: 06 INC @R0 00100000 (1) $0006 0053 01CB: 07 INC @R1 00100000 (1) $0007 0054 01CC: 08 INC R0 00100000 (1) $0008 0055 01CD: 0F INC R7 00100000 (1) $000F 0056 01CE: A3 INC DPTR 00000010 0057 01CF: 04 INC A 10000000 0058 01D0: 05 40 INC VARS 00000000 (1) $0040 0059 01D2: 26 ADD A,@R0 10010000 (1) $0006 0060 01D3: 28 ADD A,R0 10010000 (1) $0008 0061 01D4: 2F ADD A,R7 10010000 (1) $000F 0062 01D5: 22 RET 00000000 0063 01D6: 00000000 0064 01D6: 84 mycon equ $84 00000000 (2) $0084 $01D7 0065 01D6: 00000000 0066 01D6: 22 PROC5: ret 00000000 0067 01D7: 00000000 0068 01D7: B4 FE FC PROC4: cjne A,#0FEh,PROC5 10000000 (2) $00FE $01D6 0069 01DA: 85 DB 82 mov DPL,smbdat 00000000 (2) $0082 $01DB 0070 01DD: 85 DE 83 mov DPH,smbdir 00000000 (2) $0083 $01DE 0071 01E0: 85 E1 08 RDFL: mov r0,#auxbuf ; 00100000 (2) $0008 $01E1 0072 01E3: 85 E4 09 mov r1,#SMBTXSZ2 00100000 (2) $0009 $01E4 0073 01E6: E4 rdflp: clr A 10000000 0074 01E7: 93 movc A,@A+DPTR 10000000 (1) $01A9 0075 01E8: F6 mov @r0,A 01100000 (1) $0006 0076 01E9: 05 82 inc DPL 00000000 (1) $0082 0077 01EB: 08 inc r0 00100000 (1) $0008 0078 01EC: D9 F8 D5 EF 55 djnz r1,rdflp 00100000 (2) $0009 $01E6 0079 01F1: 85 F2 F3 mov smbTxRd,#SMBTXSZ 00000000 (2) $01F3 $01F2 0080 01F4: 22 ret 00000000 0081 01F5: B4 FD DF PROC3: cjne A,#0FDh,PROC4 10000000 (2) $00FD $01D7 0082 01F8: 85 F9 82 mov DPL,#TITLE ; 00000000 (2) $0082 $01F9 0083 01FB: 85 F8 83 mov DPH,#TITLE ; 00000000 (2) $0083 $01F8 0084 01FE: 80 E0 jmp RDFL 00000000 (1) $01E0 0085 0200: B4 03 F2 PROC2A: cjne A,#3,PROC3 10000000 (2) $0003 $01F5 0086 0203: 85 A5 00 mov P1MDOUT,#0 00000000 (2) $0000 $00A5 0087 0206: 85 90 FF mov P1,#0FFh 00000000 (2) $00FF $0090 0088 0209: 22 ret 00000000 0089 020A: B4 02 F3 PROC2: cjne A,#2,PROC2A 10000000 (2) $0002 $0200 0090 020D: 85 DA A5 mov P1MDOUT,smbdat 00000000 (2) $00A5 $01DA 0091 0210: 22 ret 00000000 0092 0211: B4 01 F6 PROC1: cjne A,#1,PROC2 10000000 (2) $0001 $020A 0093 0214: 85 DA 90 mov P1,smbdat 00000000 (2) $0090 $01DA 0094 0217: 85 DD A5 mov P1MDOUT,smbdir ;00000000 (2) $00A5 $01DD 0095 021A: 22 ret 00000000 0096 021B: 00000000 0097 021B: PROCESS_SMB: 00000000 0098 021B: 85 EF 1C mov A,smbcmd 10000000 (1) $021C 0099 021E: 44 00 orl A,#0 10000000 (1) $0000 0100 0220: 70 EF jnz PROC1 00000000 (1) $0211 0101 0222: 22 ret 00000000 0102 0223: 00000000 0103 0223: 80 F6 SJMP PROCESS_SMB 00000000 (1) $021B 0104 0225: 80 F4 JMP PROCESS_SMB 00000000 (1) $021B 0105 0227: 02 40 00 JMP $40 00000000 (1) $0040 0106 022A: 00000000 ASMEND *** SYMBOL TABLE *** $00AD PID $00B6 RID $000C RXD0 $00A5 K1 $0040 VARS $0180 MAIN $01A6 FWD $01A9 @A+DPTR $0084 MYCON $01D6 PROC5 $01D7 PROC4 $01DA SMBDAT $01DD SMBDIR $01E0 RDFL $01E0 AUXBUF $01E3 SMBTXSZ2 $01E6 RDFLP $01F1 SMBTXRD $01F1 SMBTXSZ $01F5 PROC3 $01F8 TITLE $0200 PROC2A $020A PROC2 $0211 PROC1 $021B PROCESS_SMB $021B SMBCMD ok TAQOZ#
Comments
Quite impressive. To help you with this, I've attached the minor-edited input, and .LST output, from a std 8051 Assembler.
Finds DA mnemonic should be DA A, and some byte-orders look wrong, and (of course) gives errors on your predefined symbols, but overall very good.
JC, JB, JC, JZ, JNZ SJMP all look ok,
JNB uses wrong opcode
JB.JNB with hex immediate format seems to emit 80h, but when using bit names they look ok ?
LJMP,LCALL have wrong byte orders
MOV rn,#i should be 2 byte opcodes
DJNZ rn,Adr seems to emit 5 bytes
If you can match those predefined names, to what is in the standard EFM8 include file, the code becomes more portable.
Yeah, me too. One of those things is going to be another attempt to get along better with Forth.
Peter, I am consistently impressed with what you get Tachyon, TAQOS to do. Nice work on explanation and "friendliness" I am able to follow more examples more easily today, than just a year ago.
Type the snippet in, use TAQOS, spin, whatever, and others can use them near directly in many cases.
Yes, so after this little adventure I will look at the P2 assembler and this will also allow us to create CODE words (as well) that work just like other Forth and so you can pass and read parameters on the stack and put the word in a test loop or just put it to work.
Slightly distracting but it is pretty close to being usable and so i will probably finish it off while i am testing BB8 on P2D2!