irmen/prog8

Calling `cx16.memory_decompress` does not work if the target is VERA's data port

Closed this issue · 14 comments

Three different ways to do it in main.start, the simplest one does not work as expected for some reason, but making the call in asm works, so it's probably not a ROM issue.

You have to uncomment each variant.

%zeropage basicsafe

main {
	sub start() {
		sys.set_irqd()

		; 1. decompress directly to vera: machine hangs
		;cx16.vaddr(1, $f000, 0, 1)
		;void cx16.memory_decompress(font, cx16.VERA_DATA0)

		; 2. decompress to buffer first: works
		;uword buf = memory("buf", 2048, 1)
		;cx16.vaddr(1, $f000, 0, 1)
		;void cx16.memory_decompress(font, buf)
		;uword i = buf
		;repeat 2048 {
		;	cx16.VERA_DATA0 = @(i)
		;	i++
		;}

		; 3. decompress directly to vera, my code: works
		;decomp_vera(font, $f0, 1)

		sys.clear_irqd()
	}

	asmsub decomp_vera(uword data @R0, ubyte addrm @Y, ubyte addrh @A) {
		%asm {{
			pha
			phx

			ora #%00010000 ; autoincr 1

			stz cx16.VERA_ADDR_L
			sty cx16.VERA_ADDR_M
			sta cx16.VERA_ADDR_H

			lda #<cx16.VERA_DATA0
			sta cx16.r1
			lda #>cx16.VERA_DATA0
			sta cx16.r1+1

			jsr cx16.jsrfar
			.word cx16.memory_decompress
			.byte 0

			plx
			pla
			rts
		}}
	}

	font: %asm {{
		.byte $18, $af, $1c, $22, $4a, $56, $4c, $20, $1e, $00, $18, $24, $42, $7e, $42, $18, $2e, $00, $7c, $22, $22, $3c, $30, $7c, $00, $4f, $08, $40, $18, $38, $22, $1c, $00, $78, $24, $22, $f8, $1e, $24, $78, $00, $7e, $08, $78, $2d, $7e, $c0, $2a, $40, $f1, $4e, $42, $54, $42, $42, $c8, $12, $1c, $08, $f8, $20, $11, $0e, $04, $f4, $30, $44, $38, $f8, $2f, $44, $48, $70, $48, $44, $0b, $40, $48, $7e, $d8, $fa, $02, $66, $5a, $5a, $18, $42, $62, $52, $4a, $46, $bd, $42, $90, $20, $f8, $18, $24, $18, $00, $7c, $49, $7c, $b0, $23, $fb, $07, $4a, $24, $1a, $42, $c8, $1b, $3c, $42, $40, $3c, $02, $42, $3c, $00, $3e, $08, $fc, $f3, $00, $42, $2a, $3c, $fa, $11, $24, $24, $18, $18, $f9, $5a, $5a, $66, $42, $4a, $24, $b5, $49, $00, $5a, $42, $7d, $40, $58, $f9, $1f, $02, $04, $18, $20, $12, $3c, $20, $fa, $98, $3c, $00, $00, $40, $20, $10, $08, $04, $02, $00, $3c, $04, $21, $40, $5c, $4b, $2a, $af, $18, $12, $00, $10, $20, $7f, $0e, $00, $f6, $24, $00, $4f, $08, $24, $22, $8c, $21, $29, $7e, $f2, $48, $24, $d1, $f9, $1e, $28, $1c, $0a, $3c, $5b, $ec, $62, $64, $08, $10, $26, $46, $00, $30, $48, $48, $30, $4a, $44, $3a, $00, $04, $08, $c4, $22, $49, $10, $9f, $48, $00, $9a, $41, $be, $f0, $10, $20, $59, $00, $2a, $1c, $3e, $9e, $42, $af, $41, $45, $46, $a3, $43, $d3, $4f, $7e, $93, $41, $40, $51, $00, $02, $c6, $51, $20, $40, $18, $f9, $00, $46, $5a, $62, $f9, $08, $18, $28, $29, $3e, $82, $3a, $02, $0c, $30, $40, $7e, $c4, $4a, $1c, $00, $79, $04, $0c, $14, $24, $7e, $04, $04, $80, $48, $78, $3a, $61, $a8, $48, $1c, $cc, $6a, $7c, $f8, $53, $7e, $42, $86, $41, $c0, $0a, $42, $ec, $22, $48, $3e, $05, $4a, $38, $36, $02, $ec, $25, $5a, $64, $08, $10, $0e, $18, $30, $60, $30, $18, $0e, $81, $43, $7f, $5b, $70, $18, $0c, $06, $0c, $18, $70, $98, $54, $10, $00, $00, $ea, $ff, $40, $49, $30, $7f, $7f, $e7, $20, $05, $f4, $05, $07, $c8, $26, $af, $0d, $20, $ed, $04, $02, $45, $73, $e0, $10, $54, $74, $04, $03, $b0, $11, $10, $e0, $0c, $80, $f7, $74, $ff, $80, $78, $54, $01, $01, $08, $00, $06, $3f, $0c, $01, $f9, $00, $00, $3c, $7e, $4f, $3c, $9f, $10, $36, $7f, $f2, $fd, $3e, $1c, $08, $00, $40, $62, $b7, $f0, $03, $04, $69, $81, $10, $61, $0f, $4a, $81, $07, $41, $08, $62, $31, $f1, $77, $2a, $0d, $02, $fe, $41, $38, $42, $c8, $63, $30, $29, $ff, $34, $a0, $50, $f8, $22, $64, $08, $58, $24, $01, $3e, $54, $14, $14, $2a, $fe, $7f, $3f, $1f, $0f, $07, $03, $01, $0d, $f0, $ff, $63, $f0, $02, $ef, $00, $4f, $f5, $ff, $80, $34, $aa, $55, $45, $49, $43, $c0, $22, $66, $1d, $ff, $fe, $fc, $f8, $f0, $e0, $c0, $80, $03, $ff, $42, $90, $ea, $0f, $01, $e9, $0f, $23, $8f, $85, $94, $eb, $f8, $44, $96, $23, $08, $45, $63, $6e, $ff, $a1, $25, $42, $d8, $0d, $c0, $f9, $ed, $e0, $ed, $07, $46, $5f, $45, $56, $06, $66, $b1, $4e, $ff, $2c, $42, $84, $86, $f6, $20, $2d, $f8, $27, $23, $18, $af, $e3, $dd, $b5, $a9, $b3, $df, $e1, $ff, $e7, $db, $bd, $81, $bd, $18, $2e, $ff, $83, $dd, $dd, $c3, $30, $83, $ff, $4f, $08, $bf, $18, $38, $dd, $e3, $ff, $87, $db, $dd, $f8, $1e, $db, $87, $ff, $81, $08, $87, $2d, $81, $c0, $2a, $bf, $f1, $b1, $bd, $54, $bd, $bd, $c8, $12, $e3, $f7, $f8, $20, $11, $f1, $fb, $f4, $30, $bb, $c7, $f8, $2f, $bb, $b7, $8f, $b7, $bb, $0b, $bf, $48, $81, $d8, $fa, $02, $99, $a5, $a5, $18, $bd, $9d, $ad, $b5, $b9, $bd, $42, $90, $20, $f8, $18, $db, $e7, $ff, $83, $49, $83, $b0, $23, $fb, $07, $b5, $db, $e5, $42, $c8, $1b, $c3, $bd, $bf, $c3, $fd, $bd, $c3, $ff, $c1, $f7, $fc, $f3, $ff, $bd, $2a, $c3, $fa, $11, $db, $db, $e7, $e7, $f9, $a5, $a5, $99, $bd, $4a, $db, $b5, $49, $ff, $5a, $42, $7d, $40, $58, $f9, $1f, $fd, $fb, $e7, $df, $12, $c3, $df, $fa, $98, $c3, $ff, $ff, $bf, $df, $ef, $f7, $fb, $fd, $ff, $c3, $fb, $21, $40, $5c, $4b, $d5, $af, $18, $12, $ff, $ef, $df, $80, $0e, $ff, $f6, $24, $00, $4f, $08, $db, $22, $6c, $21, $29, $81, $f2, $48, $db, $d1, $f9, $e1, $d7, $e3, $f5, $c3, $5b, $ec, $9d, $9b, $f7, $ef, $d9, $b9, $ff, $cf, $b7, $b7, $cf, $b5, $bb, $c5, $ff, $fb, $f7, $c4, $22, $49, $ef, $9f, $48, $ff, $9a, $41, $be, $f0, $ef, $df, $59, $00, $d5, $e3, $c1, $9e, $42, $af, $41, $45, $46, $a3, $43, $d3, $4f, $81, $93, $41, $40, $51, $ff, $fd, $be, $51, $df, $bf, $18, $f9, $00, $b9, $a5, $9d, $f9, $f7, $e7, $d7, $29, $c1, $82, $3a, $fd, $f3, $cf, $bf, $81, $c4, $4a, $e3, $00, $79, $fb, $f3, $eb, $db, $81, $fb, $fb, $80, $48, $87, $3a, $61, $a8, $48, $e3, $23, $6a, $83, $f8, $53, $81, $bd, $86, $61, $d8, $0a, $bd, $ec, $22, $48, $c1, $05, $4a, $c7, $36, $02, $ec, $25, $5a, $64, $f7, $ef, $f1, $e7, $cf, $9f, $cf, $e7, $f1, $81, $43, $7f, $5b, $8f, $e7, $f3, $f9, $f3, $e7, $8f, $98, $54, $ef, $ff, $00, $ea, $00, $40, $49, $30, $80, $80, $e7, $20, $05, $f4, $05, $07, $c8, $26, $af, $0d, $df, $ed, $fb, $02, $45, $73, $1f, $ef, $54, $74, $fb, $fc, $b0, $11, $ef, $1f, $0c, $7f, $f7, $74, $00, $7f, $78, $54, $fe, $fe, $08, $00, $06, $3f, $0c, $fe, $f9, $00, $ff, $c3, $81, $4f, $c3, $9f, $a9, $c9, $ff, $94, $70, $c1, $e3, $08, $0d, $bf, $62, $b7, $f0, $fc, $fb, $69, $7e, $10, $61, $0f, $4a, $7e, $07, $41, $08, $62, $31, $f1, $88, $d5, $0d, $fd, $fd, $41, $38, $42, $c8, $63, $30, $09, $00, $34, $5f, $af, $f8, $22, $64, $1c, $58, $24, $fe, $c1, $ab, $eb, $eb, $2a, $fe, $80, $c0, $e0, $f0, $f8, $fc, $fe, $0d, $0f, $ff, $a7, $05, $0f, $0f, $ff, $4e, $f5, $00, $7f, $8d, $55, $ff, $45, $49, $43, $c0, $22, $86, $1d, $00, $01, $03, $07, $0f, $1f, $3f, $7f, $fc, $ff, $42, $90, $ea, $f0, $01, $e9, $f0, $23, $8f, $85, $94, $eb, $07, $44, $96, $23, $08, $45, $63, $4e, $00, $93, $25, $42, $d8, $0d, $3f, $f9, $ed, $1f, $ed, $f8, $46, $5f, $45, $56, $06, $66, $b1, $4e, $00, $2c, $42, $84, $86, $f6, $20, $2d, $07, $27, $23, $e7, $f0, $e8
	}}
}
irmen commented

why are you JSRFAR'ing to the routine?

No reason, probably a misunderstanding on my side when it's required and when not.

irmen commented

another inquiry, are you using the most recent rom?
because there has been a fix regarding memory decompress into vera recently commanderx16/x16-rom#371

I use the ROM that comes with the R42 emulator. It's working, obviously.

irmen commented

Allright, just checking to see what the environment is.

I have been following this feature and waited for it to work in the emulator, and just now started to remove the workaround from my game, so there is no doubt I have a fixed ROM. The feature works fine, just not the prog8 call.

I wonder how I managed to not see that JSRFAR blunder. When you have a hammer...

irmen commented

blunder? 😄 Anyway I think this is going to require inspecting the code generated by the compiler, assuming the kernal routine works fine in R42. Very odd that it HANGS the machine. Could there be a ROM bank issue perhaps?

	lda  cx16.VERA_DATA0
	sta  cx16.r1
	stz  cx16.r1+1
	jsr  cx16.memory_decompress

The correct code would be

	lda  #<cx16.VERA_DATA0
	sta  cx16.r1
	lda  #>cx16.VERA_DATA0
	sta  cx16.r1+1
	jsr  cx16.memory_decompress

Tested it, works after manually changing the asm output.

irmen commented

That's a yikes ! Thanks for investigating already

irmen commented

Oh- wait a minute. I think I know why that code is generated like that:

The cx16.VERA_DATA0 is not a regular variable, it's a memory-mapped variable.
So if you need to pass the address of that, you have to explicitly tell the compiler to do so.

What happens if you call it like this: void cx16.memory_decompress(font, &cx16.VERA_DATA0) ?

Ah shit, that works. Nice footgun

This could use a warning, but I have of course no clue if the situation's even possible to detect. Edit: it's an implicit ubyte -> uword conversion, maybe that's what should be warned about

I don't like closing issues in repos that don't belong to me, but the issue is solved for me and can be closed. Thanks!

irmen commented

Hmm, I'll think about this a bit. But a byte to word conversion is lossless so no reason to warn about...
then again I see why this can be a footgun

To be fair, this is pretty much the same class of mistake I would make in assembly code: like, forgetting the # in front of literals happens to me all the time. It would be a nice plus if prog8 could save me from my own stupidity