Howto try small machine code programs in BASIC ?

Hardware Hacking, Programming and Game Solutions/Cheats
jedie
Posts: 655
Joined: Wed Aug 14, 2013 12:23 pm
Location: germany
Contact:

Howto try small machine code programs in BASIC ?

Post by jedie »

Found this BASIC script in InsideTheDragon book:

Code: Select all

10 ' Machine code loader
11 ' Machine codes in hex are poked into memory
12 ' locations starting at 20001 then execed
20 READ LA  ' LA = load address (start of program)
30 READ EA  ' EA = address of first instruction
40 PA = EA  'to be executed
50 READ HB$ ' Hex constants
60 IF HB$="END" THEN 100
70 POKE PA,VAL("&H"+HB$) ' Poke value into memory
80 PA = PA + 1  ' Increment address
90 GOTO 50
100 PRINT "MACHINE CODE LOADED"
110 PRINT "LOAD ADDRESS IS ";LA;"(DEC)";
111 PRINT HEX$(LA);"(HEX)"
120 PRINT "END ADDRESS IS "; PA-1;"(DEC)";
121 PRINT HEX$(PA-1);"(HEX)"
130 PRINT "EXEC ADDRESS IS";EA;"(DEC)";
131 PRINT HEX$(EA);"(HEX)"
140 PRINT "YOU ARE ADVISED TO SAVE LOADER "
141 PRINT "BEFORE RUNNING M/C CODE"
142 'If you want to execute the loaded code
143 'immediately, you should put an
144 'EXEC EA statement here.  If you do this
145 'for this program, you lose BASIC print
146 'information
150 DATA 20001 'Load address here
160 DATA 20001 'Execute address here
165 ' You put your own machine code in hex
166 ' here to load your hand translated
167 ' programs
170 DATA 34,12 ' Machine code for the
180 DATA 86,00 ' Screen filler program
190 DATA 8E,04,00  ' given below
200 DATA A7,80
210 DATA 8C,06,00
220 DATA 25,F9
230 DATA 4C
240 DATA 81,80
250 DATA 25,Fl
260 DATA 35,92
270 DATA END
Is this "best practice" for running small test machine code programs ?
... too many ideas and too little time ... Related stuff written in Python:
Dragon 32 emulator / PyDC - Python Dragon 32 converter: https://github.com/jedie/DragonPy
DWLOAD server / Dragon-Lib and other stuff: https://github.com/6809
jedie
Posts: 655
Joined: Wed Aug 14, 2013 12:23 pm
Location: germany
Contact:

Re: Howto try small machine code programs in BASIC ?

Post by jedie »

Have disassemble the machine code from the example:

Code: Select all

170 DATA 34,12      ' PSHS 12
180 DATA 86,00      ' LDA 00
190 DATA 8E,04,00   ' LDX 04 00
200 DATA A7,80      ' STA 80
210 DATA 8C,06,00   ' CMPX 06 00
220 DATA 25,F9      ' BLO/BCS F9
230 DATA 4C         ' INCA 
240 DATA 81,80      ' CMPA 80
250 DATA 25,f1      ' BLO/BCS f1
260 DATA 35,92      ' PULS 92
Think in line 250 is it's not Fl, but f1, isn't it?

Have disassemble it with this small python script: https://gist.github.com/jedie/6765945 it used the MC6809_data_raw info from DragonPy: https://github.com/jedie/DragonPy/blob/ ... ata_raw.py
... too many ideas and too little time ... Related stuff written in Python:
Dragon 32 emulator / PyDC - Python Dragon 32 converter: https://github.com/jedie/DragonPy
DWLOAD server / Dragon-Lib and other stuff: https://github.com/6809
jedie
Posts: 655
Joined: Wed Aug 14, 2013 12:23 pm
Location: germany
Contact:

Re: Howto try small machine code programs in BASIC ?

Post by jedie »

My destination is to have some small test script for testing the 6809 register functions. To compare my 6809 emulator with a real Dragon.

I would like to run something like this (pseudo code):

Code: Select all

LDA 254
INCA
print A
INCA
print A
INCA
print A
Interesting also: print the "overflow" flag out. But how?
... too many ideas and too little time ... Related stuff written in Python:
Dragon 32 emulator / PyDC - Python Dragon 32 converter: https://github.com/jedie/DragonPy
DWLOAD server / Dragon-Lib and other stuff: https://github.com/6809
jedie
Posts: 655
Joined: Wed Aug 14, 2013 12:23 pm
Location: germany
Contact:

Re: Howto try small machine code programs in BASIC ?

Post by jedie »

Found a interesting "answer" in Dragon User 41 (Nov. 84):
The USR command can be used when your machine code routine needs to pass values to and from Basic. If your routine starts at address 32000 then the USR address is set up as follows:

DEF USR5 = 32000

To call your routine and pass a variable to it use:

X = USR05 (VARPTR(X$)) (Dragon 32)
X = USR5 (VARPTR(X$)) (Dragon 64)

VARPTR returns a 16 bit address of the variable pointer, this can be returned in the D register if your program uses JSR 35623. Then [D] will be the length of the sting X$, and [D + 2] will be the address of the start of the string.
... too many ideas and too little time ... Related stuff written in Python:
Dragon 32 emulator / PyDC - Python Dragon 32 converter: https://github.com/jedie/DragonPy
DWLOAD server / Dragon-Lib and other stuff: https://github.com/6809
Sarah
Posts: 177
Joined: Wed Apr 13, 2011 3:36 pm
Contact:

Re: Howto try small machine code programs in BASIC ?

Post by Sarah »

I wouldn't really recommend using DEF USR & VARPTR unless you really do want to "pass values to and from Basic" and even then it's a bit of a headache.

Having a BASIC loader is unnecessary unless you're using both BASIC and machine language together and especially want to have a single file (a BASIC program), e.g. for publishing on paper (and even then the version from Inside The Dragon is a little clumsy and doesn't perform checksums, so isn't "best practice" by any means). In usual circumstances you can simply save/load your machine language as binary.
jedie
Posts: 655
Joined: Wed Aug 14, 2013 12:23 pm
Location: germany
Contact:

Re: Howto try small machine code programs in BASIC ?

Post by jedie »

But that's what i what to have: Use assembler and BASIC together. So i can create "readable / commended" assembler codes. Needful of testing some small snippets.

But how to give some values back from machine code to BASIC? Or how to print out some characters in machine code?
... too many ideas and too little time ... Related stuff written in Python:
Dragon 32 emulator / PyDC - Python Dragon 32 converter: https://github.com/jedie/DragonPy
DWLOAD server / Dragon-Lib and other stuff: https://github.com/6809
jedie
Posts: 655
Joined: Wed Aug 14, 2013 12:23 pm
Location: germany
Contact:

Re: Howto try small machine code programs in BASIC ?

Post by jedie »

OK, now it worked! :D

code:

Code: Select all

10 ' MACHINE CODE LOADER
20 READ LA  ' LA = LOAD ADDRESS (START OF PROGRAM)
30 READ EA  ' EA = ADDRESS OF FIRST INSTRUCTION
40 PA = EA  'TO BE EXECUTED
50 READ HB$ ' HEX CONSTANTS
60 IF HB$="END" THEN 100
70 POKE PA,VAL("&H"+HB$) ' POKE VALUE INTO MEMORY
80 PA = PA + 1  ' INCREMENT ADDRESS
90 GOTO 50
100 EXEC EA ' AUTORUN
110 PRINT "LOAD ADDRESS:";LA
120 PRINT "END ADDRESS:"; PA-1
130 PRINT "RUN AGAIN WITH EXEC";EA
149 ' LOAD ADDRESS:
150 DATA 20001
159 ' EXECUTE ADDRESS:
160 DATA 20001
165 ' MACHINE CODE IN HEX
170 DATA 34,12      ' PSHS 12
180 DATA 86,00      ' LDA 00
190 DATA 8E,04,00   ' LDX 04 00
200 DATA A7,80      ' STA 80
210 DATA 8C,06,00   ' CMPX 06 00
220 DATA 25,F9      ' BLO/BCS F9
230 DATA 4C         ' INCA
240 DATA 81,80      ' CMPA 80
250 DATA 25,F1      ' BLO/BCS F1
260 DATA 35,92      ' PULS 92
270 DATA END
It execute the machine code directly...

The origin code doesn't work: SN ERROR in line 150:

Code: Select all

150 DATA 20001 'Load address here
But why?
... too many ideas and too little time ... Related stuff written in Python:
Dragon 32 emulator / PyDC - Python Dragon 32 converter: https://github.com/jedie/DragonPy
DWLOAD server / Dragon-Lib and other stuff: https://github.com/6809
jedie
Posts: 655
Joined: Wed Aug 14, 2013 12:23 pm
Location: germany
Contact:

Re: Howto try small machine code programs in BASIC ?

Post by jedie »

I see that there is the commented assembler code in InsideTheDragon.

I put all together:

Code: Select all

170 DATA 34,12      ' SCRFL  PSHS A,X    ; Save registers A, X
180 DATA 86,00      '        LDA $0      ; First character
190 DATA 8E,04,00   ' NXTSC  LDX $400    ; X = text screen start
200 DATA A7,80      ' PRCH   STA ,X+     ; Store character
210 DATA 8C,06,00   '        CMPX $600   ; if X == text screen end:
220 DATA 25,F9      '        BLO PRCH    ;     No, next character
230 DATA 4C         '        INCA        ; Go on to next character
240 DATA 81,80      '        CMPA 80     ; if A == 80:
250 DATA 25,F1      '        BLO NXTSC   ;     Do another screenful
260 DATA 35,92      '        PULS A,X,PC ; Restore and return
... too many ideas and too little time ... Related stuff written in Python:
Dragon 32 emulator / PyDC - Python Dragon 32 converter: https://github.com/jedie/DragonPy
DWLOAD server / Dragon-Lib and other stuff: https://github.com/6809
jedie
Posts: 655
Joined: Wed Aug 14, 2013 12:23 pm
Location: germany
Contact:

Re: Howto try small machine code programs in BASIC ?

Post by jedie »

OK, here a programm thats print "X" via machine code:

Code: Select all

10 ' MACHINE CODE LOADER
20 READ LA  ' LA = LOAD ADDRESS (START OF PROGRAM)
30 READ EA  ' EA = ADDRESS OF FIRST INSTRUCTION
40 PA = EA  'TO BE EXECUTED
50 READ HB$ ' HEX CONSTANTS
60 IF HB$="END" THEN 100
70 POKE PA,VAL("&H"+HB$) ' POKE VALUE INTO MEMORY
80 PA = PA + 1  ' INCREMENT ADDRESS
90 GOTO 50
100 PRINT "AUTORUN WITH EXEC";EA;":"
105 PRINT "-------------------------------"
110 EXEC EA ' AUTORUN
114 PRINT
115 PRINT "-------------------------------"
120 PRINT "END ADDRESS:"; PA-1
130 PRINT "RUN AGAIN WITH EXEC";EA
149 ' LOAD ADDRESS:
150 DATA 20001
159 ' EXECUTE ADDRESS:
160 DATA 20001
165 ' MACHINE CODE IN HEX
170 DATA 34,12      ' PSHS A,X       ; Save registers A, X
180 DATA 86,58      ' LDA $58        ; chr($58) == "X"
190 DATA BD,80,0C   ' JSR $800C      ; call output character routine
260 DATA 35,92      ' PULS A,X,PC    ; Restore and return
270 DATA END
But can i print the value (in this case $58 == 88) of accumulator A to screen?
... too many ideas and too little time ... Related stuff written in Python:
Dragon 32 emulator / PyDC - Python Dragon 32 converter: https://github.com/jedie/DragonPy
DWLOAD server / Dragon-Lib and other stuff: https://github.com/6809
User avatar
tormod
Posts: 416
Joined: Sat Apr 27, 2013 12:06 pm
Location: Switzerland
Contact:

Re: Howto try small machine code programs in BASIC ?

Post by tormod »

Yes, there is a routine to print the value of D. So set B to the number to print and clear A, or if you have to start out with A, clear B and do a EXG A,B.
You can find the routine in Inside the Dragon, or in Graham's ROM reference called Print Number,
Post Reply