VIC-20 Startup Sequence

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
Radical Brad
Vic 20 Devotee
Posts: 256
Joined: Sat Jun 24, 2017 8:18 pm
Website: http://www.AtomicZombie.com
Location: Kakabeka Falls, ONT
Occupation: hACKER

VIC-20 Startup Sequence

Post by Radical Brad »

Greets!

I am working on some code for a VIC-20 project that starts up as a cartridge in BLK-5.

Simple code has been working fine as expected, but as things become more complex with more zero page and stack usage, I am getting unexpected results, AKA crashes.

I think the reason is my lack of understanding of what happens when the VIC powers on, and what still might be happening even when I take full control.

Here is what I understand from what I have read so far...

VIC boots, then looks for the A0CBM sequence at location 40964 and if found, starts code at the reset vector address found at location 40960.
This seems to work fine, and I have been using this bit of code...

Code: Select all

; START ADDRESS = 40969 @ 40960 TO 40961
 .ORG 40960
 .BYTE 9,160

; RESET ADDRESS = 40969 @ 40962 TO 40963
 .ORG 40962
 .BYTE 9,160
 
; BOOT CODE = A0CBM @ 40964 TO 40968
 .ORG 40964
 .BYTE 65,48,195,194,205
 
; PROGRAM START @ 40969 TO 49151
 .ORG 40969
 
Simple code seems to work fine with this sequence, but things break down when I start using more zero page.
At one point, VIC actually came back to the console and reported "Illegal Quantity Error".
How the heck is that even possible? Basic can't possibly be running if I am in charge.

In further experimentation, I tried this after a few routines... "JMP 64802", expecting to see the VIC reset into basic.
Sometimes this worked, and sometimes it failed. Seems to have to do with what I do with zero page.

Obviously I am wrong in my understanding that nothing at all is running on the 6502 if I am am booting and taking total control.
Seems some Kernal fragment must be competing with my use of zero page. is it on some kind of timer interrupt?

Another thing that seems to happen is that if I leave my expanded SRAM as RAM (not ROM), the VIC trashes it on startup, even though my code does take control. Seems the Kernal gets in there first and does a bunch of writes and reads to see what kind of expansion memory is connected. This isn't going to work all the well considering I am stuffing said SRAM with code that needs to run!

I have about 50 VIC-20 manuals downloaded, and most are either great books on coding basic or assembly, but none get too far into what really happens under the hood.

Can someone point me to a really in depth write-up on what the VIC does on power up, and when may still be happening even if basic is not started?

I can code 6502 assembly like a second language, but there seems to be some other forces acting against me here!

Thanks,
Brad
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: VIC-20 Startup Sequence

Post by Mike »

A thorough look at the reset routine beginning at $FD22 is wholeheartedly recommended. :mrgreen:

Of course it's the KERNAL that starts up your cartridge, so at least something happens before your code runs. If you want to set up memory and I/O properly, yet still remain in control, you'll at least have to copy part of the reset routine into your own cartridge startup code.

You can't change the hardware IRQ, NMI and RESET vectors in the KERNAL at $FFFA..$FFFF. Especially, the NMI is always vectored through ($0318) *before* the cartridge entry in ($A002) is honoured and the IRQ interrupt service routine expects sensible pointers in ($0314) for IRQ and ($0316) for BRK.

For my TRON game, I went through this procedure; but I forced the RAM configuration to unexpanded (regardless what RAM expansion are possibly plugged in parallel to the game cartridge) and I disabled STOP and STOP/RESTORE.
User avatar
Radical Brad
Vic 20 Devotee
Posts: 256
Joined: Sat Jun 24, 2017 8:18 pm
Website: http://www.AtomicZombie.com
Location: Kakabeka Falls, ONT
Occupation: hACKER

Re: VIC-20 Startup Sequence

Post by Radical Brad »

Thanks Mike, seems I have a much longer journey than originally thought.
I didn't realize the 6502 wasn't fully mine once my code started.

I also don't know how the VIC would determine if ROM is installed in BLOCKS 1,2, and 3.
It can't exactly do a write and read test on those locations.
So that makes me wonder how then it would know to relocate the screen memory?

Wow... a lot to cover.
Too bad there wasn't a way (outside the VIC) to stop anything from running but my own code.

I will keep looking for a good resource.

Brad
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: VIC-20 Startup Sequence

Post by Mike »

Radical Brad wrote:I also don't know how the VIC would determine if ROM is installed in BLOCKS 1,2, and 3. It can't exactly do a write and read test on those locations. So that makes me wonder how then it would know to relocate the screen memory?
Of course a RAM check is part of the KERNAL reset routine. The RAM check is the first thing done next, when no cartridge signature was found. The routine goes by the name RAMTAS, begins at $FD8D and is called by a JSR in address $FD2F. It also determines the position of screen memory - as soon as there's RAM in BLK1, screen memory is located to $1000, otherwise at $1E00.
Too bad there wasn't a way (outside the VIC) to stop anything from running but my own code.
Well, I already mentioned you should take care of $0314..$0319 with respect to interrupts. Pretty much everything else is under your control. Just as long as the KERNAL remains in its place you can't do much about, that the whole address range $C000..$FFFF is lost for any OS-like project on the VIC-20.

Well, it's o.k. for me. I regard BASIC and KERNAL as huge library, with lots of useful routines. Nothing to 'fight' against. Just one needs to know how to use them. ;)
I will keep looking for a good resource.
Compute's "Mapping the VIC-20" should cover most of your needs here.

I have my own resource, "Data Becker VC-20 intern", in German. :mrgreen:
User avatar
Radical Brad
Vic 20 Devotee
Posts: 256
Joined: Sat Jun 24, 2017 8:18 pm
Website: http://www.AtomicZombie.com
Location: Kakabeka Falls, ONT
Occupation: hACKER

Re: VIC-20 Startup Sequence

Post by Radical Brad »

Thanks again Mike!
I just found "Mapping The VIC", and it is a great resource.

This is the routine I just came up with that seems to work so far...

Code: Select all

; ////////////////////////////////////////////////////////////////////////////////////////////
; ////////// VIC-20 INITIALIZATION SEQUENCE
; ////////////////////////////////////////////////////////////////////////////////////////////

; INITIALIZE INTERNAL RAM
 JSR 64909
 
; INITIALIZE IO VECTORS 
 JSR 64850
 
; INITIALIZE IO REGISTERS 
 JSR 65017
 
; INITIALIZE VIC HARDWARE 
 JSR 58648

; CLEAR INTERRUPTS
 CLI
 
I know there is a lot more to learn about the VIC-20 internal system, but this at least gets me to the point where I can continue on my hardware design.

With the above routine at the start of my code, everything I tried works so far.
I actually have my own cursor up on the screen and flashing!

I am now writing the code to reach out and program my new Sync Generator.

Thanks for your help.
Brad
Forbidden64
Vic 20 Hobbyist
Posts: 146
Joined: Sun Feb 28, 2016 9:59 pm
Location: CA USA

Re: VIC-20 Startup Sequence

Post by Forbidden64 »

Mike wrote:
I have my own resource, "Data Becker VC-20 intern", in German. :mrgreen:
Lucky duck! Data Becker/Abacus wrote the best commodore books of all time. Several were translated into English, but not all, sadly.
I have their 2 pass assembler program/basic assembler screen editor, and as well I typed in the 'fast tape' program from 'die cassette buch fur die commodore 64'. One can roughly get through it without being a fluid speaker of german with the help of a handy translator nearby, but thankfully it was also published in north America (mostly, there are a few humorous parts of the book where words weren't translated at all). Both are marvelous though! The thing I love best about 'fast tape' is that it is not only 10x faster, but it also preserves regular loading commands, as well as adding new commands into basic for saving.

Those guys knew their stuff!

That said, 'mapping the vic' is a must have for all US/UK users.
Post Reply