Trouble with saving data using 6502

Here you can ask questions or provide insights about how to use efficiently 6502 assembly code on the Oric.
simozzer
Private
Posts: 7
Joined: Wed Dec 06, 2023 7:26 pm

Trouble with saving data using 6502

Post by simozzer »

I've been trying to write a tracker program, and would like to write a routine to CLOAD and CSAVE the data I've used for creating bars of music.

The 6502 for the code can be invoked by calling CALL #2000, which runs the tracker.

Upon saving the data for the tune is copied to locations #9000-#9300, then a flag is stored at #8FFF so that BASIC knows what to do next.,

To try and workaround the differences in ROM addresses for v1.0 and V1.1 ROMS I thought I'd do something like:

10 HIMEM #1FFF
20 CALL #2000; // ADDRESS OF PROGRAM
30 IF PEEK(#8FFF) = 1 THEN GOSUB 1000: GOTO 10
40 IF PEEK(#8FFF) = 2 THEN GOSUB 2000: GOTO 10
1000 CLS: INPUT "ENTER SAVE FILENAME:";N$
1010 CSAVE N$, A#9000, E#9300
1020 RETURN
2000 CLS: INPUT "ENTER LOAD FILENAME:";N$
2010 CLOAD N$
2020 CALL #<ADDREESS OF ROUTINE TO COPY TUNE DATA BACK INTO WORKING AREA>
2030 RETURN.


When I try this the program dies horribly after trying CSAVE, and the basic program (which I'd previously saved using CSAVE "TRACKER", A#500, E#3FFF) dies horribly, and the text of the BASIC program becomes corrupted.

Am I doing something wrong with my attempts to CSAVE the BASIC and the machine code together, or something else completely wrong?

( https://github.com/simozzer/FourBar )

The main code can be found in main.s and Fourbar.s


I've also tried writing CSAVE in 6502 ( https://github.com/simozzer/FourBar/blo ... nt_files.s ) but this does not seem to work, and I get no file produced.


I'd welcome any help that anyone can provide here, as I seem to have become a little stuck.

Thanks in advance.

Simon
User avatar
iss
Wing Commander
Posts: 1626
Joined: Sat Apr 03, 2010 5:43 pm
Location: Bulgaria
Contact:

Re: Trouble with saving data using 6502

Post by iss »

Welcome here, @simozzer!

In "The Oric Library" you can find "Oric Advanced User Guide - ROM Disassembly section" - this is for BASIC ver.1.1 and it's very useful reference.

If we check the Appendix B (i.e. zero page usage):
Screenshot_20231207_235635.jpg
Here #9A,#9B holds the start address of the BASIC program, or

Code: Select all

PRINT HEX$(DEEK(#9A))
will return #501
So, if you want to save a BASIC program plus additional binary data together, you need to change the end address before saving or:

Code: Select all

DOKE#9C,#3FFF
CSAVE"TRACKER",AUTO
This will save the memory area from #501 to #3FFF and will mark it as BASIC auto-run program.
Also this the easiest way to be compatible with 1.0 and 1.1 ROMs.
simozzer
Private
Posts: 7
Joined: Wed Dec 06, 2023 7:26 pm

Re: Trouble with saving data using 6502

Post by simozzer »

Thanks @ISS,

Still not working quite as expected, though I suspsect I might be overwriting zero page addresses which are used elswehere.

I'll carry on digging. Much appreciated.
User avatar
iss
Wing Commander
Posts: 1626
Joined: Sat Apr 03, 2010 5:43 pm
Location: Bulgaria
Contact:

Re: Trouble with saving data using 6502

Post by iss »

simozzer wrote: Thu Dec 07, 2023 11:37 pm I'll carry on digging. Much appreciated.
I'll check you github sources too.
BTW, congrats - the tracker look cool! :D
simozzer
Private
Posts: 7
Joined: Wed Dec 06, 2023 7:26 pm

Re: Trouble with saving data using 6502

Post by simozzer »

Thanks - don't worry about looking at the sources too much yet (It's a bit of a mess, and may cause a headache).
User avatar
ibisum
Wing Commander
Posts: 1639
Joined: Fri Apr 03, 2009 8:56 am
Location: Vienna, Austria
Contact:

Re: Trouble with saving data using 6502

Post by ibisum »

Awesome thread, and welcome @simozzer!
simozzer
Private
Posts: 7
Joined: Wed Dec 06, 2023 7:26 pm

Re: Trouble with saving data using 6502

Post by simozzer »

I've been mostly able to get it working, but I'm still confused.

If I load the code, let it run, and then enter some simple BASIC (as follows) then it seems to work.

10 CALL #2000:CLS
15 DIM N$
30 IF N=1 THEN INPUT"SAVENAME";N$: CSAVE N$,A#9000,E#9300: PRINT "SAVED"
40 IF N=2 THEN INPUT"LOADNAME";N$: CLOAD N$: CALL #292C: RUN
50 PRINT "QUIT"

If I then try to save the program with
DOKE#9C,#3FFF
CSAVE"TRACKER",AUTO

Subsequent runs seem to end up with my machine code routines somehow corrupted, and I run into a JAM instruction, where previously there was valid code.

Could anyone suggest why this might be?

I've tried to do a workaround where I copy zero-page and the variables to somewhere else, before the MC is executed - the copy them back when leaving the MC section. This, however, does not work.

P.S. I'm using Oricutron, and I've attached a screenshot from the monitor of when it goes "bad".
Attachments
WhyTheJAM.png
User avatar
Symoon
Archivist
Posts: 2299
Joined: Sat Jan 14, 2006 12:44 am
Location: Paris, France

Re: Trouble with saving data using 6502

Post by Symoon »

Hi. Not quite sure but isn't there a missing HIMEM ?
simozzer wrote: Sat Dec 09, 2023 12:27 am 0 HIMEM #1FFF
10 CALL #2000:CLS
15 DIM N$
30 IF N=1 THEN INPUT"SAVENAME";N$: CSAVE N$,A#9000,E#9300: PRINT "SAVED"
40 IF N=2 THEN INPUT"LOADNAME";N$: CLOAD N$: CALL #292C: RUN
50 PRINT "QUIT"
User avatar
iss
Wing Commander
Posts: 1626
Joined: Sat Apr 03, 2010 5:43 pm
Location: Bulgaria
Contact:

Re: Trouble with saving data using 6502

Post by iss »

Indeed! +1 for HIMEM😁
simozzer
Private
Posts: 7
Joined: Wed Dec 06, 2023 7:26 pm

Re: Trouble with saving data using 6502

Post by simozzer »

Thanks but I tried HIMEM before I posted this. It sadly, also, doesn't work.

I guess I'll keep on trying.
User avatar
Symoon
Archivist
Posts: 2299
Joined: Sat Jan 14, 2006 12:44 am
Location: Paris, France

Re: Trouble with saving data using 6502

Post by Symoon »

I really don't see any reason why the memory (hence the code) after #2000 should be changed, except some BASIC managmeent by the system. HIMEM is supposed to protect the area, that's strange. Your own code doesn't self-modifiy, right?
With Oricutron and F3 key in the debugger you can display the memory, maybe worth having a look around #2000 to see how it was changed when the JAM occurs? Maybe it could help finding what made it change.
simozzer
Private
Posts: 7
Joined: Wed Dec 06, 2023 7:26 pm

Re: Trouble with saving data using 6502

Post by simozzer »

Thanks for the input so far.

@Symoon - there is a small amount of self-modifcation for a few LDA and STA instructions.

However:
If load the main 6502 code TAP file from my repo then the tracker runs automatically.

I can then quit the app (by pressing Q) and load the BASIC runner, which does contain the HIMEM instruction, and RUN it then everything works as expected (except for a small error with clearing the input field when entering and saving file names).

Code: Select all

 1 HIMEM #1FFF
 2 CALL #2000: CLS: DIM N$: P = PEEK(#8FFF)
 3 IF P=1 THEN INPUT"SAVE NAME?";N$: CSAVE N$, A#9000,E#9300: GOTO 2
 4 IF P=2 THEN INPUT "LOAD NAME?"; N$:CLOAD N$: CALL #2932: GOTO 2
 5 PRINT "BYE"
@Iss - It's when I try to do the following (to save both the BASIC and 6502 code as 1 file) that it fails.

DOKE #9C, #3FFF
CSAVE "AUTORUN", AUTO

I get an out of memory error on the HIMEM line (HIMEM #1fff) when I try to run the program. (ouch!)

I suspect there is something about the CSAVE/CLOAD which is setting the memory upper limit to #3FFF and not letting me lower it.

Can anyone suggest a workaround? I've uploaded both .TAP files to my repo, if that might help


Links to TAP files:
================
MAIN 6502 code: (Tracker.tap) https://github.com/simozzer/FourBar/blo ... racker.tap
Basic Code: (BASICRUN.tap) https://github.com/simozzer/FourBar/blo ... SICRUN.tap
Last edited by simozzer on Wed Dec 20, 2023 5:24 am, edited 2 times in total.
simozzer
Private
Posts: 7
Joined: Wed Dec 06, 2023 7:26 pm

Re: Trouble with saving data using 6502

Post by simozzer »

I think I've found a resolution, just after writing the above post, so I thought I'd post it here:

If I change the HIMEM from "HIMEM #1FFF", to "DOKE #9C, #1FFF" then I can set the HIMEM value as I desire (without actually calling HIMEM) and the program now mostly works.

Code: Select all

1 DOKE #9C, #1FFF: REM ***INSTEAD OF HIMEM #1FFF***
 2 CALL #2000: CLS: DIM N$: P = PEEK(#8FFF)
 3 IF P=1 THEN INPUT"SAVE NAME?";N$: CSAVE N$, A#9000,E#9300: GOTO 2
 4 IF P=2 THEN INPUT "LOAD NAME?"; N$:CLOAD N$: CALL #2932: GOTO 2
 5 PRINT "BYE"
I can now successfully save the program with

DOKE #9C, #3FFF
CSAVE "FOURBAR",AUTO


A version that now works, and can load and save the data is available at https://github.com/simozzer/FourBar/blo ... OURBAR.tap

Thanks for all your input folks!
Post Reply