Wednesday, April 11, 2018

Patching the CoCo ROM Part 7

Spent a little time working on a looping version of the multiply code in order to fit it into the same number of bytes as the original code.  Instead of making a patch program I just stuck the bytes directly into the ROM file with a hex editor.

The first version fit once part of it replaced the MICROSOFT text at the end of the sine coefficient table, but the results were wrong.  The fix pushed it over the available space by a few bytes.  Two or three opcodes need to be eliminated in order to fit.  I'll sleep on it and see if I can cut the size a little more.


Monday, April 9, 2018

Patching the CoCo ROM Part 6

The multiply code that will be patched is located at $BB03 in the Color BASIC ROM.
It multplies the FPA1 mantissa by accumulator B, and adds the result to FPA2.

The original code (below) is 43 bytes in length.  I'm working on a looping version using the hardware multiply.  While it will be much faster than the current ROM, it won't be quite as fast as the code from the MC-10.  It just has to fit into those 43 bytes if it's going to work.

;* MULTIPLY FPA1 MANTISSA BY ACCB AND
;* ADD PRODUCT TO FPA2 MANTISSA
LBB03 LDA FPA2 ; GET FPA2 MS BYTE
RORB ; ROTATE CARRY FLAG INTO SHIFT COUNTER;
;* DATA BIT INTO CARRY
BEQ LBB2E ; BRANCH WHEN 8 SHIFTS DONE
BCC LBB20 ; DO NOT ADD FPA1 IF DATA BIT = 0
LDA FPA2+3 ; * ADD MANTISSA LS BYTE
ADDA FPA1+3 ; *
STA FPA2+3 ; *
LDA FPA2+2 ; = ADD MANTISSA NUMBER 3 BYTE
ADCA FPA1+2 ; =
STA FPA2+2 ; =
LDA FPA2+1 ; * ADD MANTISSA NUMBER 2 BYTE
ADCA FPA1+1 ; *
STA FPA2+1 ; *
LDA FPA2 ; = ADD MANTISSA MS BYTE
ADCA FPA1 ; =
LBB20 RORA ; * ROTATE CARRY INTO MS BYTE
STA FPA2 ; *
ROR FPA2+1 ; = ROTATE FPA2 ONE BIT TO THE RIGHT
ROR FPA2+2 ; =
ROR FPA2+3 ; =
ROR FPSBYT ; =
CLRA ; CLEAR CARRY FLAG
BRA LBB03 ; KEEP LOOPING
LBB2E RTS

Patching the CoCo ROM Part 5

In order to shorten the size of the patch for the new parser address, I'm going to attempt patching everything in the ROM area that matches a call to CHRGET and CHRGOT.
Fraught with peril may be the best way to describe this, as it may patch code or data that just happens to have the same byte sequence.  However, it may require less code to come up with a list of exclusions not to patch, than a list of addresses to patch.  CHRGET has 32 hits, and CHRGOT has 29 hits in the Color BASIC disassembly alone.

The way the ROM would be patched, is simply to look for the opcode for a JSR or JMP to the direct page at address $9F (CHRGET) and $A5 (CHRGOT).  The opcode for a JSR using direct addressing is $9D, and the opcode for JMP is $0E.  So it's just a matter of searching through the ROM in a loop looking for those values followed by $9F or $A5.  To make sure it won't patch something it shouldn't, I'll perform a search for those byte sequences in the ROM binaries using a hex editor, then look at what is at the address in the ROM disassembly.  Anything that shouldn't be patched will be added to a list of addresses to exclude.

Thursday, April 5, 2018

Patching the CoCo ROM Part 4

*REVISED*


While this will require patching everywhere CHRGET and CHRGOT are called with the new addresses, it will allow the CoCo parser patch to be as fast as the MC-10 version except when it passes a byte boundary.  255 out of 256 cases is good enough for me.

I looked at calling a function to automatically patch calls from the ROM, but there isn't enough room for the jumps to the rest of the code even if there are no JMPs to CHRGET the code.

org $9F
OLDCHRGET
bra CHRGET         ;catch any unpatched calls to CHRGET
INCHIGHBYTE       ;code to increment the high byte of the current address being parsed
inc CHRGOT+1
bra CHRGOT
OLDCHRGOT
bra CHRGOT        ;catch any unpatched calls to CHRGOT



org $F3
CHRGET                 ; increment BASIC code pointer, then fall through to CHRGOT
inc CHRGOT+2
beq INCHIGHBYTE
CHRGOT                 ; read the current byte of BASIC code we are pointing at
lda >0000              ; self modifying code, the actual read takes place here

*LINE OF CODE IS MISSING HERE*

bcs PARSER2       ;
rts
PARSER2
jmp >BROMHK+4


*edit*
Always double check your code after cutting and pasting!  This is one of the most common causes of bugs in a program.  Doing so saved me a lot of headaches with this code.

Please note that after adding the missing line of code, this patch is too large for the available space at $F3 without changes.  That prompted this revision.  It no longer catches calls to the old CHRGET and CHRGOT addresses, but there is room to catch one of them if I decide to do so.


org $9F
INCHIGHBYTE       ;code to increment the high byte of the current address being parsed
inc CHRGOT+1
bra CHRGOT
PARSER2
  jmp >BROMHK+4  ; test for other characters

org $F3
CHRGET                 ; increment BASIC code pointer, then fall through to CHRGOT
inc CHRGOT+2

beq INCHIGHBYTE
CHRGOT                 ; read the current byte of BASIC code we are pointing at
lda >0000              ; self modifying code, the actual read takes place here

cmpa      #':' ; set Z flag if statement separator or larger
bcs PARSER2       ; 
rts

Patching the CoCo ROM Part 3

Super Extended BASIC seems to have 1012 unused bytes starting at $FA0C according to the disassembly.  The hardware multiply, and code to multiply by 1/10 should fit there.  There should be room for a few more patches.

Patching the CoCo ROM Part 2

After some research, it appears that the CHRGET and CHRGOT functions are followed immediately by system variables.  This means the default location cannot be used. 
There are 13 unused bytes at $F3, but it isn't enough space for the full patch.  The shorter version could be moved there though, and the JMP #### could be replaced with a BRA ##.  It would be faster, but not as fast as the MC-10 patch due to the extra BRA.  It still saves clock cycles but I'm guessing it will be less than 1%, where the MC-10 patch saved 1.2% or more.

   org $A8
   bra  $F3


   org $F3
cmpa #'9+1 ; IS THIS CHARACTER >=(ASCII 9)+1?
bcs PARSER2 ; (BHS is the same as BCC, reverse is BCS)
rts ; return if character >= (ASCII 9) +1
PARSER2
jmp >BROMHK+4


A rewrite of the ROM would let us move CHRGET to right before $F3 and the entire function could fit.  But compatibility would be sacrificed.  Why Microsoft didn't do this in the first place we'll never know, but it seems odd.

Sunday, April 1, 2018

Patching the CoCo ROM Part 1

The TRS-80 Color Computer's Color BASIC ROM is quite similar to the MC-10's Microcolor BASIC.  The major flaws found in the latter, also seem to be in the former.

(this has been revised since the first posting)
The parser uses the same CHRGET/CHRGOT functions to step through programs.  The CHRGET code is stored in the ROM at $A10D, right after a series of other bytes that are copied to direct page RAM at the same time.  The code is copied to address $9F, and CHRGOT simply skips incrementing the address so the interpreter can reload the current byte being examined by the parser.  After reading a character, it jumps to the ROM code at $AA19 to test the character and decide how to process it.

CHRGET
;increment the BASIC parser's pointer
INC CHARAD+1
BNE LA123
INC CHARAD
CHRGOT
; load the current character
LA123 LDA >0000
JMP >BROMHK



ROM portion at $AA19

BROMHK
CMPA #'9+1 ; IS THIS CHARACTER >=(ASCII 9)+1?
BHS LAA28 ; BRANCH IF > 9; Z SET IF = COLON
CMPA #SPACE ; SPACE?
BNE LAA24 ; NO - SET CARRY IF NUMERIC
JMP GETNCH ; IF SPACE, GET NEXT CHAR (IGNORE SPACES)
LAA24 SUBA #'0 ; * SET CARRY IF
SUBA #-'0 ; * CHARACTER > ASCII 0
LAA28 RTS



Patching the code to reduce or eliminate jumps back and forth between CHRGET and ROM simply requires overwriting the JMP opcode with part or all of the ROM code, but adapted for the short branch back to CHRGET.  This also overwrites whatever is on the direct page until the end of the patch.  Anything that uses those addresses including BASIC, Extended BASIC, and Super Extended BASIC is likely to cause the computer to crash.  There is no guarantee either of these patches will work.

First approach, the entire function is in RAM:

org $9F
CHRGET
org $a8

CMPA #'9+1 ; IS THIS CHARACTER >=(ASCII 9)+1?
BHS EXIT ; BRANCH IF > 9; Z SET IF = COLON (BCC)
CMPA #SPACE ; SPACE?
BNE ASCII ; NO - SET CARRY IF NUMERIC
BRA CHRGET ; IGNORE SPACES
ASCII SUBA #'0 ; * SET CARRY IF
SUBA #-'0 ; * CHARACTER > ASCII 0
EXIT RTS



Second approach, returns on most likely condition, only uses 3 additional bytes:

org $9F
CHRGET
org $a8

cmpa #'9+1 ; IS THIS CHARACTER >=(ASCII 9)+1?
bcs PARSER2 ; (BHS is the same as BCC, reverse is BCS)
rts ; return if character >= (ASCII 9) +1
PARSER2
jmp >BROMHK+4 ;a9-ac