How to detect a CBM machine from basic ?

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
nbla000
Salmon Run
Posts: 2582
Joined: Thu Oct 13, 2005 8:58 am
Location: Italy

How to detect a CBM machine from basic ?

Post by nbla000 »

Hi,

For my next project, I need a simple basic program (V.2+ compatible) to detect the CBM machine where the program is running.

This is a sample of my program:

Code: Select all

1 A=0:B=PEEK(65494):REM $FFD6 - LOAD KERNAL JUMP TABLE (LOW BYTE)
2 IFB=101THENA=1:PRINT"C128"
3 IFB=255THENA=2:PRINT"C16-PLUS/4":REM SHOULD BE 67 ????
4 IFB=158THENA=3:PRINT"C64":REM HOW TO DETECT THE 64DTV ?
5 IFB=66THENA=4:PRINT"VIC20"
6 REM B= 70 ; PET KERNAL 1
7 REM B=194 ; PET KERNAL 2
8 REM B=  1 ; PET KERNAL 4
9 REM B= 26 ; CBM II SHOULD BE RAM ???
10 IFA=0THENPRINT"YOU CANNOT USE THIS MACHINE"
I mainly need to detect the Vic-20 and the C64, probably the C16-PLUS4 then the C128, nothing else.

The code looks on the low byte of the LOAD kernal JUMP TABLE (at least for machines that I need to check) In vice works but there are some strange things...

Problem 1:
C16-PLUS/4 the code in $FFD5 is JMP $F043 [$4C $43 $F0] so the $FFD6 value (65494) should be 67 [$43] but the programs report 255 [$FF] why ?

Problem 2:
I wish to detect the 64DTV so by looking on $FFD6 from a real C64 or from a 64DTV it always report 158, may I detect the 64DTV in some way ?

Problem 3:
I don't need to detect CBM II machines but I wish to understand to prevent problems, $FFD5 should contains RAM, in monitor the command fill $ffd6 $ffd6 $50 writes the value $50 on location $ffd6 but the basic programs always report 26, it's a VICE bug or is normal ?

Someone has another idea to detect a CBM machine from basic ?

Thanks.
Mega-Cart: the cartridge you plug in once and for all.
Bacon
for breakfast
Posts: 578
Joined: Mon Apr 19, 2004 8:07 am

Re: How to detect a CBM machine from basic ?

Post by Bacon »

nbla000 wrote:C16-PLUS/4 the code in $FFD5 is JMP $F043 [$4C $43 $F0] so the $FFD6 value (65494) should be 67 [$43] but the programs report 255 [$FF] why ?
My guess is that the C16/Plus4 banks in RAM when you do a PEEK, so you're not seeing the KERNAL at all. You should ask the guys who hang out at the forum on Plus/4 World. They should know.

Edit: My guess seems to be correct; if you POKE e.g. 128 into 65494 you get 128 back when you PEEK the adress.
Bacon
-------------------------------------------------------
Das rubbernecken Sichtseeren keepen das cotton-pickenen Hands in die Pockets muss; relaxen und watschen die Blinkenlichten.
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

Based on the VICE emulator, it seems the Plus/4 returns ROM data at $FFFC/FD. That is the pointer to the cold start routine on all 6502 compatible computers. Since all Commodore computers tend to have the cold start at a different location, it may be useful to PEEK(65532) instead of 65494:

VIC-20 = 34
C64/C64DTV = 226
Plus/4 = 246
C128 = 61

CBM-II = 151
PET 2001 = 56
PET 3032 = 209
PET 4032/8032/8096/8296/SuperPET = 22

By the way, the CBM-II has a paged memory map in 15 banks. Basic works in bank 1 but ROMs are in bank 15, as well as a small amount of RAM. That may be why you get different results from PEEK than what you stored in the monitor.

It makes it cumbersome to write machine code programs for those computers as either you have to restrict yourself to a few kilobytes of RAM (sounds familiar) or you have to completely avoid making ROM calls. You could of course bank switch yourself before making the calls but I don't know what happens to the program counter.. It appears quite limiting for machines with 128-256K of RAM to only have a fraction of it available for own machine code programs. It is even possible to add an internal memory expansion (!) to extend that amount of RAM, but it would make the computer non-standard.
Anders Carlsson

Image Image Image Image Image
User avatar
nbla000
Salmon Run
Posts: 2582
Joined: Thu Oct 13, 2005 8:58 am
Location: Italy

Post by nbla000 »

Not sure, it seems an easy solution but...

From the plus4maps at project64

$FF40-$FFFF 65344-65535 RAM (under ROM)
$FF40-$FFFF 65344-65535 Operating system ROM

what does it means ? it seems that may coesist RAM and ROM in $FF40-$FFFF


Btw if you type POKE65532,55 then PEEK(65532) reports 55 not 246 so it's RAM not a secure way to check this machine...

From another document seems that by writing any value to the location $FF3E you select ROM and on $FF3F you select RAM but nothing seems that happen apart a freeze if I write on $FF3F...

Another strange thing... if I poke a LM command in RAM example:

Code: Select all

1000   AD D6 FF   LDA $FFD6
1003   60         RTS
and then type SYS4096
A reports 43 as awaited while this code writes to ram:

Code: Select all

1000   A9 90      LDA #$90
1002   8D D6 FF   STA $FFD6
1005   60         RTS
SYS4096
PRINTPEEK(65494)
reports 144 ($90) !?!?

It seems a little bit complicated machine, we need to understand how it works...


ED: An addition this code read from ram and rom without freezes but i need to switch to RAM then to ROM otherwhise the machine freezes...

Code: Select all

.C:1000   A9 45      LDA #$45
.C:1002   8D 3F FF   STA $FF3F
.C:1005   AD D6 FF   LDA $FFD6 ;<-- report $FF (RAM)
.C:1008   8D 3E FF   STA $FF3E
.C:100b   AD D6 FF   LDA $FFD6 ;<-- report $43 (ROM)
.C:100e   60         RTS
Last edited by nbla000 on Thu Sep 03, 2009 5:22 am, edited 1 time in total.
Mega-Cart: the cartridge you plug in once and for all.
User avatar
Mike
Herr VC
Posts: 4845
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

I remember, that on C16, plus/4, machine programs started with SYS execute with ROM banked in.

Thus a simple solution would be a small ML program, that reads address $FFFC. One slight complication occurs, because the addresses to transfer the registers for a SYS call are located at other addresses on C16/+4.

But this one should work. I placed the routine at a position in the stack which should normally be free:

Code: Select all

$0140 LDA $FFFC
$0143 STA $0147
$0146 RTS
SYS 320. Then PEEK(327) should return the contents of $FFFC in ROM.

Greetings,

Michael

P.S.: On a C128 you'll face a similar problem, if not BANK15 is selected, but instead one the BANKs 0 or 1. This also affects SYS instructions. And a "stray" POKE from a program that banks in ROM on the C16 will overwrite a routine located in the $FF00 page on the C128. And, the other way round, 'LDA #0:STA $FF00', which maps in ROM on the C128 will write to a TED register on the C16. :(
User avatar
nbla000
Salmon Run
Posts: 2582
Joined: Thu Oct 13, 2005 8:58 am
Location: Italy

Post by nbla000 »

Mike wrote:

Code: Select all

$0140 LDA $FFFC
$0143 STA $0147
$0146 RTS
SYS 320. Then PEEK(327) should return the contents of $FFFC in ROM.
Thanks Mike, this is a great idea since 100/1FF is the stack area for all Commodore machine that I wish to check and from the basic prompt the stack pointer is about on $f0...

Now there is just a last question, how to detect a 64DTV instead of a standard C64 ?
Last edited by nbla000 on Thu Sep 03, 2009 9:59 am, edited 1 time in total.
Mega-Cart: the cartridge you plug in once and for all.
User avatar
nbla000
Salmon Run
Posts: 2582
Joined: Thu Oct 13, 2005 8:58 am
Location: Italy

Post by nbla000 »

nbla000 wrote:how to detect a 64DTV instead of a standard C64 ?
I've found some information here and by looking basic code of some c64dtv specific programs, I've discovered how to detect it.

This is the final basic program that works on all machines emulated by VICE.

Code: Select all

10 FORA=0TO6:READB:POKE320+A,B:NEXT
20 SYS320:A=0:B=PEEK(327)
30 IFB<>226THEN60
40 A=1:POKE53311,1:B=PEEK(53312):POKE53311,0:IFB=255THENA=2
50 GOTO200
60 IFB<>34THEN100
70 A=3:B=PEEK(44):IFB=4THENA=4
80 IFB=18THENA=5
90 GOTO200
100 IFB=246THENA=6
110 IFB=61THENA=7
200 FORB=0TO7:READC$(B):NEXT
210 PRINT"SYSTEM: "C$(A)
250 DATA173,252,255,141,71,01,96
300 DATA "NOT SUPPORTED","C64","C64DTV","VIC UNEXPANDED"
310 DATA "VIC +3K","VIC +8K OR PLUS","C16-PLUS4","C128"
Thank you very much for all your tips.
Mega-Cart: the cartridge you plug in once and for all.
Post Reply