Understanding 6502 assembly on the Commodore 64 - (10) Disappearing, Reappearing BASIC

Every time we test our program, we are in fact using BASIC to facilitate the initialization.  By calling SYS 49152, BASIC is handing over control to the machine language program, which we have loaded into memory at 49152 (HEX $C000).  At this point BASIC is no longer useful to us.









BASIC owns some prime real estate on the C64, it lives in memory area $A000 to $BFFF. Taking up a total of 8K.  You'll be disappointed to know that if you write a program, or attempt to store anything in this area, your changes won't be written.

In its default configuration, Memory area $A000 to $BFFF is mapped to BASIC 8K ROM.  Yes, read only memory.  This is the machine language program that is BASIC.  As luck would have it, We can easily remove this ROM and make this memory area available as regular RAM.  This is done at memory address $01, way down in the zero page area.

Doing this not only do we make available the 8K that BASIC took up, but the next memory location after this , $C000 (where our program lives) through $CFFF was 4K already available to us.  This gives us a grand total of 12K of contiguous memory for a machine language program!!!

I know what your thinking.... If this is true, why not remove basic and start our program at $A000 instead of $C000.   Well, you can't, because the instructions within our program that disable the ROM would have to be written in the memory area where the ROM currently exists in order to remove it.

Weird huh?




$0001 - Processor port. Bits:
  • Bits #0-#2: Configuration for memory areas $A000-$BFFF, $D000-$DFFF and $E000-$FFFF. Values:
    • %x00: RAM visible in all three areas.
    • %x01: RAM visible at $A000-$BFFF and $E000-$FFFF.
    • %x10: RAM visible at $A000-$BFFF; KERNAL ROM visible at $E000-$FFFF.
    • %x11: BASIC ROM visible at $A000-$BFFF; KERNAL ROM visible at $E000-$FFFF.
    • %0xx: Character ROM visible at $D000-$DFFF. (Except for the value 0, see above.)
    • %1xx: I/O area visible at $D000-$DFFF. (Except for the value %100, see above.)
  • Bit #3: Datasette output signal level.
  • Bit #4: Datasette button status; 0 = One or more of PLAY, RECORD, F.FWD or REW pressed; 1 = No button is pressed.
  • Bit #5: Datasette motor control; 0 = On; 1 = Off.
Default: $37, 00110111


By changing the last two bits from 11 to 10 we keep everything mapped the same except for the BASIC rom, which we turn into RAM

lda #$36
sta $01

and yes, as previously discussed, we could have used a binary representation to do this as well.
(added a space for the web browser)

lda #% 00110110
sta $01

Lets have a look at the memory right before we run the program with SYS 49152


(C:$e5cf) m a000 bfff
>C:a000  94 e3 7b e3  43 42 4d 42  41 53 49 43  30 a8 41 a7   ..{.CBMBASIC0.A.
>C:a010  1d ad f7 a8  a4 ab be ab  80 b0 05 ac  a4 a9 9f a8   ................
>C:a020  70 a8 27 a9  1c a8 82 a8  d1 a8 3a a9  2e a8 4a a9   p.'.......:...J.
>C:a030  2c b8 67 e1  55 e1 64 e1  b2 b3 23 b8  7f aa 9f aa   ,.g.U.d...#.....
>C:a040  56 a8 9b a6  5d a6 85 aa  29 e1 bd e1  c6 e1 7a ab   V...]...).....z.
>C:a050  41 a6 39 bc  cc bc 58 bc  10 03 7d b3  9e b3 71 bf   A.9...X...}...q.
>C:a060  97 e0 ea b9  ed bf 64 e2  6b e2 b4 e2  0e e3 0d b8   ......d.k.......
>C:a070  7c b7 65 b4  ad b7 8b b7  ec b6 00 b7  2c b7 37 b7   |.e.........,.7.
>C:a080  79 69 b8 79  52 b8 7b 2a  ba 7b 11 bb  7f 7a bf 50   yi.yR.{*.{...z.P
>C:a090  e8 af 46 e5  af 7d b3 bf  5a d3 ae 64  15 b0 45 4e   ..F..}..Z..d..EN
>C:a0a0  c4 46 4f d2  4e 45 58 d4  44 41 54 c1  49 4e 50 55   .FO.NEX.DAT.INPU
>C:a0b0  54 a3 49 4e  50 55 d4 44  49 cd 52 45  41 c4 4c 45   T.INPU.DI.REA.LE
>C:a0c0  d4 47 4f 54  cf 52 55 ce  49 c6 52 45  53 54 4f 52   .GOT.RU.I.RESTOR
>C:a0d0  c5 47 4f 53  55 c2 52 45  54 55 52 ce  52 45 cd 53   .GOSU.RETUR.RE.S
>C:a0e0  54 4f d0 4f  ce 57 41 49  d4 4c 4f 41  c4 53 41 56   TO.O.WAI.LOA.SAV
>C:a0f0  c5 56 45 52  49 46 d9 44  45 c6 50 4f  4b c5 50 52   .VERIF.DE.POK.PR
>C:a100  49 4e 54 a3  50 52 49 4e  d4 43 4f 4e  d4 4c 49 53   INT.PRIN.CON.LIS
>C:a110  d4 43 4c d2  43 4d c4 53  59 d3 4f 50  45 ce 43 4c   .CL.CM.SY.OPE.CL
>C:a120  4f 53 c5 47  45 d4 4e 45  d7 54 41 42  a8 54 cf 46   OS.GE.NE.TAB.T.F
>C:a130  ce 53 50 43  a8 54 48 45  ce 4e 4f d4  53 54 45 d0   .SPC.THE.NO.STE.
>C:a140  ab ad aa af  de 41 4e c4  4f d2 be bd  bc 53 47 ce   .....AN.O....SG.

Now lets look at the same area while its running the program

(C:$f140) m a000 bfff
>C:a000  55 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00   U...............
>C:a010  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00   ................
>C:a020  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00   ................
>C:a030  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00   ................
>C:a040  ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ................
>C:a050  ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ................
>C:a060  ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ................
>C:a070  ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ................
>C:a080  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00   ................
>C:a090  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00   ................
>C:a0a0  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00   ................
>C:a0b0  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00   ................
>C:a0c0  ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ................
>C:a0d0  ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ................
>C:a0e0  ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ................
>C:a0f0  ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ................
>C:a100  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00   ................
>C:a110  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00   ................
>C:a120  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00   ................
>C:a130  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00   ................
>C:a140  ff ff ff ff  ff ff ff ff  ff ff ff ff  ff ff ff ff   ................


See that, BASIC is gone.  Well the question now becomes the following..........

What would happen if in our program I press the E key which we wrote to end the program and return to BASIC?  The answer is, you'll be right back at BASIC's ready prompt like nothing happened.  The reason is, BASIC warm started when the program ended, and within the warm-start procedure, moved the BASIC ROM back into A000-BFFF, destroying anything that was previously there

Its worth pointing out, that using the MONITOR on TFC3, you cannot go in and change the value of $01 on the fly,  it will automatically reset itself each time.  Probably as a control to stop a catastrophic failure of the system.


Also, did you notice the easter egg at $A004 through A00B. It says CBMBASIC, This does absolutely nothing, and has no functionality, it just takes up space.


Lastly, did you notice BIT 5.   Yes this is the bit that automatically stopped our DATTASETTE when a program loaded, by turning the BIT to 1 from 0



NEXT----->



No comments:

Post a Comment