Jump to content
IGNORED

Relocation in the 850 Handler


tschak909

Recommended Posts

I am trying to understand how the macros in the 850 handler are actually handling marking code for relocation. Specifically, we have a small table of equates for opcodes:

 

;       OPCODE DEFINITIONS FOR RELOCATING MACRO
RBIT    .EQU $2C
RJSR    .EQU $20
RLDAX   .EQU $BD
RSTAX   .EQU $9D
RCPX    .EQU $EC
RLDA    .EQU $AD
RSTX    .EQU $8E
RJMP    .EQU $4C
RSTA    .EQU $8D
RLDAY   .EQU $B9
RSTAY   .EQU $99
RINCX   .EQU $FE
RCMPX   .EQU $DD
RADCX   .EQU $7D
RLDYX   .EQU $BC
RINC    .EQU $EE
RSTY    .EQU $8C
RORAX   .EQU $1D
RLDY    .EQU $AC
RJMPI   .EQU $6C
RLDX    .EQU $AE
RDEC    .EQU $CE
RANDX   .EQU $3D
RSBCX   .EQU $FD

And a macro that acts upon them:

;       RELOCATION MACRO
REL     .MACRO  OP,VARNUM
        .BYTE   OP
        .BYTE   VARNUM
        .BYTE   *-Q+1
Q       .SET *
        .ENDM

And, as it so happens, because Q has to be initialized once, the first relocation placeholder is done by hand:

RHOPEN  .BYTE $20
        .BYTE 0,0
Q       .SET *

And subsequent calls are done using the macro, e.g. this is equivalent:

RHCLOS  REL RJSR,0     TEST DEVICE NUMBER FOR VALIDITY

I moved the macro into my own code thusly:

                  ATARI Macro Assembler Ver  1.0C  Page   3
  N: CIO HANDLER                                       D2:RELOC.MAC       
  RELOCATION MACROS                                 

  0000              
                           ; OPCODES
                    
        = 002C      RBIT   EQU     $2C
        = 0020      RJSR   EQU     $20
        = 00BD      RLDAX  EQU     $BD
        = 009D      RSTAX  EQU     $9D
        = 00EC      RCPX   EQU     $EC
        = 00AD      RLDA   EQU     $AD
        = 008E      RSTX   EQU     $8E
        = 004C      RJMP   EQU     $4C
        = 008D      RSTA   EQU     $8D
        = 00B9      RLDAY  EQU     $B9
        = 0099      RSTAY  EQU     $99
        = 00FE      RINCX  EQU     $FE
        = 00DD      RCMPX  EQU     $DD
        = 007D      RADCX  EQU     $7D
        = 00BC      RLDYX  EQU     $BC
        = 00EE      RINC   EQU     $EE
        = 008C      RSTY   EQU     $8C
        = 001D      RORAX  EQU     $1D
        = 00AC      RLDY   EQU     $AC
        = 006C      RJMPI  EQU     $6C
        = 00AE      RLDX   EQU     $AE
        = 00CE      RDEC   EQU     $CE
        = 003D      RANDX  EQU     $3D
        = 00FD      RSBCX  EQU     $FD
                    
                    REL    MACRO   OP,VARNUM
                           DB      %1
                           DB      %2
                           DB      *-Q+1
                    Q      SET     *
                            ENDM

And yet, when I use the macro as intended, I am getting output that doesn't make sense. What's actually going on here? I've attached a listing where I have replaced the JSR GDIDX call with an REL RJSR,0 call. 

 

handler-reloc-test.txt

 

Is this macro working as intended? How am I supposed to fix up this location?

 

-Thom

 

Link to comment
Share on other sites

So still fumbling around here, but I noticed all of the values in the original source for "VARNUM" are even.  So it looks like there's a relocation table, and possibly the code for the actual relocator both missing.  Or I just haven't seen them yet.

Link to comment
Share on other sites

Well there's something wonky about the addressing math. The "= *-Q+1" values in the listing are not correct. $0728-$0729+1 = 0 not $06. The only way it seems to make sense is if Q is the last Q, not the one in the current macro iteration. So that makes it the distance from the last relocated element, which seems a bit odd in itself.

Link to comment
Share on other sites

1 minute ago, evilmoo said:

I started poking at the internal 850 ROM for additional bits, and it appears there there's still some source missing somewhere.  I don't want to set you back to square one, but have you tried looking at the XEP80-related files?

 

https://atariwiki.org/wiki/Wiki.jsp?page=XEP80#section-XEP80-RelocatorSource

Don't worry about square 1. I am technically at square 1, right now, trying to understand how best to do this. :) I've now seen three approaches, and am trying to ddx between them to understand what needs to be done.

-Thom

Link to comment
Share on other sites

No, they should all be zero. Apparently though, in AMAC, label references are not local to the macro. So yes, they all appear to be chained together and presumably the offsets are added to some base address to patch in the correct relocated addresses. As you say though the actual relocater (BOOTB/HANDLB etc.) appears to be missing from the source listings, so it's not all that useful.

Link to comment
Share on other sites

As an aside, I did run the ACTION! based relocator program, assembling two versions of my N: handler one page apart, and merged with the relocator (after taking the table generated by relgen and merging it into the relocator code), and merging the relocated binary onto the compiled result... The result was quite comical (somehow the banner offsets got skewed and I got a line of hearts...)

 

Will try again, when I get a few free cycles, I'm in the middle of trying to figure out work related stuff atm (I'm pretty much on call 24 hours a day, so I have to take my free cycles the moment I find them)

 

-Thom

  • Like 1
Link to comment
Share on other sites

1 hour ago, tschak909 said:

Now that I think about it, I need to look at Avery's RHND850 replacement, as there is a relocator there (it's in the Altirra sources)

My relocator works differently, the references in the instructions are stored as offsets and there are separate delta-encoded relocation tables for low-byte, high-byte, and word relocations. It doesn't require a list of targets and can encode references to anywhere in the segment. Generating the relocations is more involved, as it involves assembling the module four times. Special macros are still required but only for high-byte references. The relocation processor is written in C++ although it would be simple to replicate in any other language.

Link to comment
Share on other sites

1 hour ago, tschak909 said:

I know I'm gonna regret asking, but...

 

@phaeron why four times? :)

Base assembly to $2800, one at $2801 to detect low byte changes, one at $A800 to detect high byte changes, and a fourth at $2800 with the hi-byte macros changed to encode the low byte offset used to calculate the high byte. Differencing the latter three against the base assembly gives the three types of relocations.

  • Like 3
Link to comment
Share on other sites

Base assembly to $2800, one at $2801 to detect low byte changes, one at $A800 to detect high byte changes, and a fourth at $2800 with the hi-byte macros changed to encode the low byte offset used to calculate the high byte. Differencing the latter three against the base assembly gives the three types of relocations.
Wow.

Sent from my SM-G920F using Tapatalk

Link to comment
Share on other sites

3 hours ago, tschak909 said:

Now that I think about it, I need to look at Avery's RHND850 replacement, as there is a relocator there (it's in the Altirra sources)

 

-Thom

 

I did some work on this a few years ago as I wanted the ability to build the 850 ROM code from the source Curt provided, but obviously the relocation code is missing.  My guess is they had some helper program generate the offsets in the relocator table.  This is part of the source I use with an automated build script that generates the 850 ROM in five phases (relocatable handler, handler arithmetic checksum, relocator, relocator arithmetic checksum, and main code).  The only part that is missing is dynamically generating the offsets in the table (right now it is hard-coded), although some of the values I can get from the ATASM label dump.

 

DDEVIC = $0300
DBUFLO = $0304
DBUFHI = $0305
DBYTLO = $0308
DBYTHI = $0309
DTIMLO = $0306
DUNIT  = $0301
DSTATS = $0303
DCOMND = $0302
SIOV   = $E459
MEMLO	 = $02E7
DOSINI = $000C

ZPTMP	 = 		$80
ORIGIN = 		$0500

		  *= ORIGIN

			.BYTE   0,3             ; FLAGS, SECTOR COUNT
      .WORD   ORIGIN          ; ENTRY ADDRESS
      .WORD   $E4C0           ; ADDRESS OF RTS INSTRUCTION IN OS !!!!!!!!!!

			LDA #$50
			STA DDEVIC
			LDA MEMLO
			STA DBUFLO
			LDA MEMLO+1
			STA DBUFHI
			; Original 850 Handler is 1425 bytes in size
			LDA #< HANDLL
			STA DBYTLO
			LDA #> HANDLL
			STA DBYTHI
			STA DTIMLO
			LDA #$01
			STA DUNIT
			LDA #$40
			STA DSTATS
			LDA #$26
			STA DCOMND
			JSR SIOV
			BPL RELOC
			SEC
			RTS

RELOC
      LDX # RELOCTABLEND - RELOCTABL
ADJOFFSET
      CLC
      LDA RELOCTABL-2,X
      ADC MEMLO
			STA RELOCTABL-2,X
			LDA RELOCTABL-1,X
			ADC MEMLO+1
			STA RELOCTABL-1,X
			DEX
			DEX
			BNE ADJOFFSET

			LDY #$00
			STY TEMP1
			JSR SETADDR

			; Substitute variables
LOOP1
			LDA (ZPTMP),Y
			TAX
			INY
			LDA (ZPTMP),Y
			PHA
			LDA VARTABL+1,X
			STA (ZPTMP),Y
			DEY
			LDA VARTABL,X
			STA (ZPTMP),Y
			PLA
			JSR DECADDR
			BNE LOOP1

			LDX #$02
			JSR SETADDR
LOOP2
			LDA (ZPTMP),Y
			TAX
			LDA VARTABL,X
			STA (ZPTMP),Y
			LDX TEMP1
			INC TEMP1
			LDA DELTATABL,X
			JSR DECADDR
			BNE LOOP2

			LDX #$04		; set WRMXIT
			JSR SETADDR
			LDA DOSINI
			STA (ZPTMP),Y
			INY
			LDA DOSINI+1
			STA (ZPTMP),Y
			LDA RINVEC
			STA DOSINI
			LDA RINVEC+1
			STA DOSINI+1
			LDX #$0C
			; JMP to handler start
			.BYTE $4C

RELOCTABL
			.WORD $03E9  ;RINCLD

VARSTART	; $05AB
      .word $058F  ; RVECTS+4
      .word $04C2  ; PREPT+2
	    .word $03FC  ; WRMXIT+1

VARTABL
			.WORD $0543  ;RDVTST	0
			.WORD $069C  ;IOCTRL-1	2
		  .WORD $0694  ;PUTCTR-1	4
		  .WORD $0698  ;RERROR-1	6
		  .WORD $06BD  ;ACTSTR	8
		  .WORD $04B4  ;PREPUT	10
		  .WORD $06BC  ;ALLDUN	12
		  .WORD $06BA  ;RMAINV	14
		  .WORD $06BB  ;RMAINV+1	16
		  .WORD $06B4  ;RVSAVE	18
		  .WORD $06B3  ;OUSTAT	20
	  	.WORD $055B  ;MAKDCB	22
		  .WORD $06BE  ;RSCHAR	24
		  .WORD $06A0  ;RTRANS-1	26
		  .WORD $00C8  ;PAROUT	28
		  .WORD $04F0  ;CLHIBT	30
		  .WORD $04D2  ;PARITY	32
		  .WORD $0502  ;X32M1	34
		  .WORD $0611  ;OUTBAS	36
		  .WORD $04F9  ;CIRCIN	38
		  .WORD $0515  ;CMP16B	40
		  .WORD $06AF  ;NXTOUT	42
		  .WORD $06B0  ;NXTOUT+1	44
		  .WORD $06B1  ;INSTAT	46
		  .WORD $06B2  ;INSTAT+1	48
		  .WORD $0525  ;CIN16B	50
		  .WORD $06A4  ;RSPRCH-1	52
		  .WORD $06B0  ;NXTOUT+1	54
		  .WORD $0582  ;JMPTABL+1 56
		  .WORD $0581  ;JMPTABL	58
		  .WORD $06AB  ;BOBUF	60
		  .WORD $06A9  ;TOBUF	62
		  .WORD $06AC  ;BOBUF+1	64
		  .WORD $06AA  ;TOBUF+1	66
		  .WORD $0536  ;CINTRY	68
		  .WORD $0690  ;GETCTR-1	70
		  .WORD $06BC  ;ALLDUN	72
		  .WORD $058B  ;RVECTS	74
		  .WORD $0115  ;PTBFUL	76
		  .WORD $04DD  ;SETRNS	78
		  .WORD $054F  ;SPCDCB	80
		  .WORD $0223  ;STFUNK	82
		  .WORD $0571  ;HANDVS   84
		  .WORD $0413  ;RIN	86
		  .WORD $06AD  ;NEXTIN	88
		  .WORD $06AE  ;NEXTIN+1	90
		  .WORD $050C  ;INC16B	92
	    ; These are offsets from ORIGIN
      ; HANDVS table
      .WORD $FFFF  ;RHOPEN-1
		  .WORD $0025  ;RHCLOS-1
		  .WORD $015E  ;RHGET-1
		  .WORD $006D  ;RHPUT-1
		  .WORD $020C  ;RHSTAT-1
		  .WORD $0256  ;RHSPEC-1
	    ; JMPTBL
		  .WORD $0370  ;FORCE-1
		  .WORD $0395  ;CONTRL-1
		  .WORD $0389  ;CONFIG-1
		  .WORD $039C  ;TRANSL-1
		  .WORD $0271  ;STREAM-1

		  .WORD $0473  ;ROUT
RINVEC	 .WORD $03B3  ;RINWRM
	    .WORD $0591  ;INBASE
	    .WORD $03FE  ;RBREAK
RELOCTABLEND

DECADDR
			STA TEMP2
			SEC
			LDA ZPTMP
			SBC TEMP2
			STA ZPTMP
			BCS DECXIT
			DEC ZPTMP+1
DECXIT
      LDA TEMP2
			RTS

SETADDR
			LDA VARSTART,X
			STA ZPTMP
			LDA VARSTART+1,X
			STA ZPTMP+1
			RTS

DELTATABL ; Table at $064B - what are these?
		  .BYTE $0A
			.BYTE $E6
			.BYTE $05
			.BYTE $85
			.BYTE $0B
			.BYTE $48
			.BYTE $05
			.BYTE $37
			.BYTE $05
			.BYTE $00

TEMP1	.DS 1		;$0655
TEMP2	.DS 1		;$0656

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...