BASIC Program on Cartridge

You need an actual VIC.

Moderator: Moderators

SparkyNZ
Vic 20 Enthusiast
Posts: 183
Joined: Tue Jan 18, 2011 2:23 am

BASIC Program on Cartridge

Post by SparkyNZ »

I'd like to be able to switch on my Vic and have BASIC program loaded from a cartridge/ROM.

What would be the easiest way to do this? I have cartridge that I've made which will accept a ROM at $A000. I'm assuming one way this could be done would be to have a small machine code program at $A000 that would copy BASIC program data into the BASIC RAM space and possibly set up BASIC start/end pointers.

Is there anything available that will do this job already? (ie. assemble a blob of memory from a BASIC program and provide a loader at $A000) ? (ie. something for WIndows/Linux etc)

I do have an SD2IEC but I just want a quick means of being able to boot directly into a BASIC program.


(mod: thread moved from Hardware section to Emulation and Cross Development as it ultimately does not concern a hardware issue but the handling of non-VIC-20-native programming tools)
User avatar
Mike
Herr VC
Posts: 5130
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: BASIC Program on Cartridge

Post by Mike »

SparkyNZ wrote:I'm assuming one way this could be done would be to have a small machine code program at $A000 that would copy BASIC program data into the BASIC RAM space and possibly set up BASIC start/end pointers.
That pretty much sums up what I did here: a BASIC game as cartridge. :)
SparkyNZ
Vic 20 Enthusiast
Posts: 183
Joined: Tue Jan 18, 2011 2:23 am

Re: BASIC Program on Cartridge

Post by SparkyNZ »

Mike wrote: Mon Mar 31, 2025 2:47 amThat pretty much sums up what I did here: a BASIC game as cartridge. :)
Thanks Mike. I'll take a look after work. That will really help a lot! :-)
SparkyNZ
Vic 20 Enthusiast
Posts: 183
Joined: Tue Jan 18, 2011 2:23 am

Re: BASIC Program on Cartridge

Post by SparkyNZ »

Mike wrote: Mon Mar 31, 2025 2:47 amThat pretty much sums up what I did here: a BASIC game as cartridge. :)
I thought I'd try baby steps first before burning an EPROM.. I've just assembled a tiny machine code program to flash the screen:

Code: Select all

*=$a000

  BYTE $10, $A0 ; Reset vector
  BYTE $09, $A0 ; RESTORE key vector

  BYTE $41, $30, $C3, $C2, $CD        ; A0CBM Signature

        BIT $9111   ; Acknowledge NMI
        JMP $FF56   ; End NMI
        NOP
; A010
start
        INC $900F
        jmp start
I assembled this with CBMPrgStudio and naively thought I could just rename the .prg output to .crt and load into VICE to test it out. I guess the actual Vice .crt file format would actually be required. What's the easiest way to convert into a format that Vice will treat as a generic cartridge?

I could just burn the EPROM but I'd like to have Vice working as a testbed before I start playing with actual hardware.

Ohh!! Maybe this is what I'm looking for:

https://vice-emu.sourceforge.io/vice_15.html

..but I obviously don't know how to drive it :-)

Code: Select all

W:\Vic20Dev\VicRom1>cartconv -t bin -i VicRom1.prg -o VicRom1.crt
Error: Illegal file size of VicRom1.prg

W:\Vic20Dev\VicRom1>cartconv -t prg -i VicRom1.prg -o VicRom1.crt -l A000
Error: Illegal file size of VicRom1.prg
FYI, the contents of the .PRG file look like this:
VicRom1.png

Code: Select all

00000000  10 A0 09 A0 41 30 C3 C2 CD 2C 11 91 4C 56 FF EA  . . A0ÃÂÍ,.‘LVÿê
00000010  EE 0F 90 4C 10 A0                                î..L. 
User avatar
Mike
Herr VC
Posts: 5130
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: BASIC Program on Cartridge

Post by Mike »

SparkyNZ wrote:I assembled this with CBMPrgStudio and naively thought I could just rename the .prg output [...]
For testing purposes, a *.prg with start address $A000 is entirely sufficient. You'd soft-load the file to RAM in BLK5 and start the cartridge code with a reset (on VICE, a soft-reset: that one doesn't clear RAM).
[...] to .crt and load into VICE to test it out. I guess the actual Vice .crt file format would actually be required. [...]
No. VICE also accepts raw *.bin files (in that case you have to give the start address in the cartridge menu), or smart-attach for *.prg files (in that case VICE per default takes the load address from the file).

You'll have to pad the file for full 4 KB, 8 KB or 16 KB, though (plus 2 bytes for *.prg files).
What's the easiest way to convert into a format that Vice will treat as a generic cartridge?
If you insist on an emulator format, that is (as you've already found out) cartconv in the VIC bin suite.
I could just burn the EPROM but I'd like to have Vice working as a testbed before I start playing with actual hardware.
That's always a good idea, but the hardware of course also has to be right.
Ohh!! Maybe this is what I'm looking for: [...] ..but I obviously don't know how to drive it :-) [...]
cartconv insists on files padded up to the actual size of the EPROM.

...

Cartridge code has the full responsibility to init the VIC-20. Actually, your "minimal" version is not sufficient at all, as it does not properly initialize the KERNAL vectors on start-up. The cartridge NMI vector actually relies on $0318 pointing to the KERNAL NMI routine, thus your cartridge code will crash upon tapping the RESTORE key.

To be able to run BASIC programs, the cartridge reset code has to replicate a good part of the KERNAL reset routine. This is what I did with my sample game cartridge. A piecemeal strategy ("just do a bit and see if it runs") does not work, see here: Cartridge Auto Start Tips?
groepaz
Vic 20 Scientist
Posts: 1265
Joined: Wed Aug 25, 2010 5:30 pm

Re: BASIC Program on Cartridge

Post by groepaz »

cartconv insists on files padded up to the actual size of the EPROM.
use -p option to ignore that :)

i'd use .prg files and then use "generic cartridge", thats fine for testing and works in most common cases
I'm just a Software Guy who has no Idea how the Hardware works. Don't listen to me.
SparkyNZ
Vic 20 Enthusiast
Posts: 183
Joined: Tue Jan 18, 2011 2:23 am

Re: BASIC Program on Cartridge

Post by SparkyNZ »

OK, I've had another go:

Code: Select all

*=$a000

  BYTE $10, $A0 ; Reset vector
  BYTE $09, $A0 ; RESTORE key vector

  BYTE $41, $30, $C3, $C2, $CD        ; A0CBM Signature

        BIT $9111   ; Acknowledge NMI
        JMP $FF56   ; End NMI
        NOP
; A010
        JSR $FD8D   ; RAMTAS - INITMEM
        LDA #$10    ; Force BASIC into
        STA $0282   ; unexpanded RAM
        LDA #$1E    ; ...
        STA $0284   ; ...
        STA $0288   ; ...
        JSR $FD52   ; RESTOR
        LDA #$C2    ; Disable
        STA $0314   ; STOP key
        JSR $FDF9   ; INITVIA - and interrupts
        JSR $E518   ; INISK - initialise VIC, screen and pointers
        CLI

; A02F
start
        INC $900F
        jmp start
groepaz wrote: Tue Apr 01, 2025 12:22 pm use -p option to ignore that :)

i'd use .prg files and then use "generic cartridge", thats fine for testing and works in most common cases
I couldn't get "-t prg" to work - cartconv gave an error saying it was already in binary format.

I tried using the "-p" option and "-t vic20" but I'm getting confusing output:

Code: Select all

W:\Vic20Dev\VicRom1>cartconv -p -t vic20 -i VicRom1.prg -o VicRom1.crt -l A000
Error: Can't read VicRom1.prg
Input file: VicRom1.prg
Output file: VicRom1.crt
Conversion from binary format to VIC20 Generic VIC20 Cartridge .crt successful.
It's definitely generated something. I don't know if the .prg extension is confusing it or not.

I've tried the old 2.4 version of Vice that I normally use as well as the 3.9 GTK version. The 3.9 GTK version is showing this:
gtk_cart1.png
Both versions say that the cartridge is an invalid format. The VicRom1.crt file is 2128 bytes:
crt_dump.png
Mike wrote: Tue Apr 01, 2025 12:14 am Cartridge code has the full responsibility to init the VIC-20. Actually, your "minimal" version is not sufficient at all, as it does not properly initialize the KERNAL vectors on start-up. The cartridge NMI vector actually relies on $0318 pointing to the KERNAL NMI routine, thus your cartridge code will crash upon tapping the RESTORE key.

I'm not yet ready to play around with loading BASIC programs yet. Right now I just want to create something simple with these goals:
1) Prove I can generate and load a cartridge image in Vice
2) Test out my PCB with a simple ROM (cartridge) image

..then I'll knock up my BASIC program and add it into the mix with your example code. I hope I have enough initialisation to reach the flashing of the screen now.
groepaz
Vic 20 Scientist
Posts: 1265
Joined: Wed Aug 25, 2010 5:30 pm

Re: BASIC Program on Cartridge

Post by groepaz »

What i meant is... make a binary in .prg format (with load address 0xa000 in this case) and then simply attach in VICE as "generic cartridge" - no need to deal with cartconv for this
I'm just a Software Guy who has no Idea how the Hardware works. Don't listen to me.
SparkyNZ
Vic 20 Enthusiast
Posts: 183
Joined: Tue Jan 18, 2011 2:23 am

Re: BASIC Program on Cartridge

Post by SparkyNZ »

groepaz wrote: Thu Apr 03, 2025 4:13 pm What i meant is... make a binary in .prg format (with load address 0xa000 in this case) and then simply attach in VICE as "generic cartridge" - no need to deal with cartconv for this
Is that what CBMPrgStudio does by default? I thought the .prg output from CBMPrgStudio was just a raw binary file. Sorry I'm not really all that familiar with the formats. I honestly couldn't say whether a .prg file has a header of some sort or not. That gives me something else to look into after work again though. Thanks

Update: OK, so PRG format is just a 2byte header with the start address in little-endian format. I'll make sure Vice gets that and give it another go.
SparkyNZ
Vic 20 Enthusiast
Posts: 183
Joined: Tue Jan 18, 2011 2:23 am

Re: BASIC Program on Cartridge

Post by SparkyNZ »

groepaz wrote: Thu Apr 03, 2025 4:13 pm What i meant is... make a binary in .prg format (with load address 0xa000 in this case) and then simply attach in VICE as "generic cartridge" - no need to deal with cartconv for this
CBMPrgStudio outputs a .PRG file but doesn't include the 2-byte header (I confirmed this by assembling a project that only contained an RTS and it was simply 1 byte long containing $60).

I added 00 A0 to the start of my test code as shown below but VICE keeps telling me it's an "Invalid cartridge image" when I try "File->Attach cartridge image...->Generic image..."

What am I doing wrong?
prg_header.png
UPDATE:
Aha! I think I got this - "Attach cartridge" doesn't work - but if I drag the .prg into Vice (with BLK5 enabled) and do Alt-R, it runs!

AND.. I was using CBMPrgStudio 3.5.14.. The latest 4.5.0 puts the 2-byte PRG header into the output now.. Just to further confuse me :-)
SparkyNZ
Vic 20 Enthusiast
Posts: 183
Joined: Tue Jan 18, 2011 2:23 am

Re: BASIC Program on Cartridge

Post by SparkyNZ »

OK, so now that I have the "load PRG into BLK5" method working - how can I create a .crt file with cartconv?

Is the input file supposed to be .prg or just raw binary (no 2-byte header) ?

Code: Select all

W:\Vic20Dev\VicRom1>cartconv -p -t vic20 -i VicRom1.bin -o VicRom1.crt -l a000
Error: Can't read VicRom1.bin
Input file: VicRom1.bin
Output file: VicRom1.crt
Conversion from binary format to VIC20 Generic VIC20 Cartridge .crt successful.

W:\Vic20Dev\VicRom1>cartconv -p -t vic20 -i VicRom1.prg -o VicRom1.crt
Error: Can't read VicRom1.prg
Input file: VicRom1.prg
Output file: VicRom1.crt
Conversion from binary format to VIC20 Generic VIC20 Cartridge .crt successful.
What does the error "Can't read VicRom1.prg" mean? It must have read the file because the contents of it have been copied into the .crt below:

Code: Select all

00000000  56 49 43 32 30 20 43 41 52 54 52 49 44 47 45 20  VIC20 CARTRIDGE 
00000010  00 00 00 40 02 00 00 00 00 00 00 00 00 00 00 00  ...@............
00000020  47 65 6E 65 72 69 63 20 56 49 43 32 30 20 43 61  Generic VIC20 Ca
00000030  72 74 72 69 64 67 65 00 00 00 00 00 00 00 00 00  rtridge.........
00000040  43 48 49 50 00 00 08 10 00 00 00 00 00 00 08 00  CHIP............
00000050  00 A0 10 A0 09 A0 41 30 C3 C2 CD 2C 11 91 4C 56  . . . A0ÃÂÍ,.‘LV
00000060  FF EA 20 8D FD A9 10 8D 82 02 A9 1E 8D 84 02 8D  ÿê .ý©..‚.©..„..
00000070  88 02 20 52 FD A9 C2 8D 14 03 20 F9 FD 20 18 E5  ˆ. Rý©Â... ùý .å
00000080  58 EE 0F 90 4C 2F A0 FF FF FF FF FF FF FF FF FF  Xî..L/ ÿÿÿÿÿÿÿÿÿ
Why is Vice rejecting the .crt file? I must have something wrong with the command-line parameters surely?

I've attached my .crt, .prg and .bin files if it helps.
Attachments
VicRom1.zip
(680 Bytes) Downloaded 9 times
SparkyNZ
Vic 20 Enthusiast
Posts: 183
Joined: Tue Jan 18, 2011 2:23 am

Re: BASIC Program on Cartridge

Post by SparkyNZ »

I've also tried padding the input files up to 8192 bytes. This now produces no errors/warnings in both cartconv or Vice when I try to load the .crt file but the cartridge isn't auto starting??

From bin file (no 2-byte header):

Code: Select all

W:\Vic20Dev\VicRom1>cartconv -t vic20 -i bin.pad -o VicRom1.crt -l a000
Input file: bin.pad
Output file: VicRom1.crt
Conversion from binary format to VIC20 Generic VIC20 Cartridge .crt successful.

Code: Select all

00000000  56 49 43 32 30 20 43 41 52 54 52 49 44 47 45 20  VIC20 CARTRIDGE 
00000010  00 00 00 40 02 00 00 00 00 00 00 00 00 00 00 00  ...@............
00000020  47 65 6E 65 72 69 63 20 56 49 43 32 30 20 43 61  Generic VIC20 Ca
00000030  72 74 72 69 64 67 65 00 00 00 00 00 00 00 00 00  rtridge.........
00000040  43 48 49 50 00 00 20 10 00 00 00 00 00 00 20 00  CHIP.. ....... .
00000050  10 A0 09 A0 41 30 C3 C2 CD 2C 11 91 4C 56 FF EA  . . A0ÃÂÍ,.‘LVÿê
00000060  20 8D FD A9 10 8D 82 02 A9 1E 8D 84 02 8D 88 02   .ý©..‚.©..„..ˆ.
00000070  20 52 FD A9 C2 8D 14 03 20 F9 FD 20 18 E5 58 EE   Rý©Â... ùý .åXî
00000080  0F 90 4C 2F A0 FD FF FF FF FF FF FF FF FF FF FF  ..L/ ýÿÿÿÿÿÿÿÿÿÿ
From .prg file:

Code: Select all

W:\Vic20Dev\VicRom1>cartconv -t vic20 -i prg.pad -o VicRom1.crt
Input file: prg.pad
Output file: VicRom1.crt
Conversion from binary format to VIC20 Generic VIC20 Cartridge .crt successful.

Code: Select all

00000000  56 49 43 32 30 20 43 41 52 54 52 49 44 47 45 20  VIC20 CARTRIDGE 
00000010  00 00 00 40 02 00 00 00 00 00 00 00 00 00 00 00  ...@............
00000020  47 65 6E 65 72 69 63 20 56 49 43 32 30 20 43 61  Generic VIC20 Ca
00000030  72 74 72 69 64 67 65 00 00 00 00 00 00 00 00 00  rtridge.........
00000040  43 48 49 50 00 00 20 10 00 00 00 00 00 00 20 00  CHIP.. ....... .
00000050  00 A0 10 A0 09 A0 41 30 C3 C2 CD 2C 11 91 4C 56  . . . A0ÃÂÍ,.‘LV
00000060  FF EA 20 8D FD A9 10 8D 82 02 A9 1E 8D 84 02 8D  ÿê .ý©..‚.©..„..
00000070  88 02 20 52 FD A9 C2 8D 14 03 20 F9 FD 20 18 E5  ˆ. Rý©Â... ùý .å
00000080  58 EE 0F 90 4C 2F A0 FD FF FF FF FF FF FF FF FF  Xî..L/ ýÿÿÿÿÿÿÿÿ
I unchecked BLK5 in the Vic20 Settings. What's stopping the .crt file from auto starting now?
User avatar
Mike
Herr VC
Posts: 5130
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: BASIC Program on Cartridge

Post by Mike »

SparkyNZ wrote:CBMPrgStudio outputs a .PRG file but doesn't include the 2-byte header (I confirmed this by assembling a project that only contained an RTS and it was simply 1 byte long containing $60).
For use with the KERNAL load routine, a *.prg file is expected to include the load address. Whatever CBMPrgStudio emits as raw or bin file, it shouldn't use *.prg as extension.

From bin file (no 2-byte header): [...]

Code: Select all

00000000  56 49 43 32 30 20 43 41 52 54 52 49 44 47 45 20  VIC20 CARTRIDGE 
00000010  00 00 00 40 02 00 00 00 00 00 00 00 00 00 00 00  ...@............
00000020  47 65 6E 65 72 69 63 20 56 49 43 32 30 20 43 61  Generic VIC20 Ca
00000030  72 74 72 69 64 67 65 00 00 00 00 00 00 00 00 00  rtridge.........
00000040  43 48 49 50 00 00 20 10 00 00 00 00 00 00 20 00  CHIP.. ....... .
00000050  10 A0 09 A0 41 30 C3 C2 CD 2C 11 91 4C 56 FF EA  . . A0ÃÂÍ,.‘LVÿê
00000060  20 8D FD A9 10 8D 82 02 A9 1E 8D 84 02 8D 88 02   .ý©..‚.©..„..ˆ.
00000070  20 52 FD A9 C2 8D 14 03 20 F9 FD 20 18 E5 58 EE   Rý©Â... ùý .åXî
00000080  0F 90 4C 2F A0 FD FF FF FF FF FF FF FF FF FF FF  ..L/ ýÿÿÿÿÿÿÿÿÿÿ
At offset 50, this dump correctly starts with the four bytes of the cold- and warmstart entries (and offset 54 shows the A0CBM signature). The other dump from the *.prg file shows $00 $A0 from the load address, this one can't work (besides not having the A0CBM signature at the right place).

When you have attached the *.crt file in VICE, does the chip content even show up in BLK5 when you invoke the monitor?

(as an aside note: you probably thwarted the auto detection of the *.prg header when you padded the *.bin payload up to 8192 bytes but then cropped the *.prg file from 8194 bytes back to 8192 bytes. Then, from the file size, cartconv can only assume it's a *.bin file and is bound to mistake the load address for actual chip data. If you'd do that with a full EPROM dump in *.prg format, the last two bytes of the ROM data would be missing!)
groepaz
Vic 20 Scientist
Posts: 1265
Joined: Wed Aug 25, 2010 5:30 pm

Re: BASIC Program on Cartridge

Post by groepaz »

is that what CBMPrgStudio does by default? I thought the .prg output from CBMPrgStudio was just a raw binary file.
No idea, i have only briefly checked it years ago :)
I added 00 A0 to the start of my test code as shown below but VICE keeps telling me it's an "Invalid cartridge image" when I try "File->Attach cartridge image...->Generic image..."
Oh. Its probably still checking if the file is really 8k then - so you'll have to pad it. something like !fill $c000 - *
Aha! I think I got this - "Attach cartridge" doesn't work - but if I drag the .prg into Vice (with BLK5 enabled) and do Alt-R, it runs!
Caution: this will load the file to ram and run from ram. I don't recommend doing this when coding, especially not as a beginner (too easy to accidently produce something that will not actually work as rom)
I'm just a Software Guy who has no Idea how the Hardware works. Don't listen to me.
SparkyNZ
Vic 20 Enthusiast
Posts: 183
Joined: Tue Jan 18, 2011 2:23 am

Re: BASIC Program on Cartridge

Post by SparkyNZ »

Mike wrote: Fri Apr 04, 2025 3:14 am When you have attached the *.crt file in VICE, does the chip content even show up in BLK5 when you invoke the monitor?
crt_monitor.png

This doesn't look good does it? :-) It looks as though it isn't parsing the .crt header and actually loading the file like a raw file. I'll sleep on it..
Post Reply