Beginning ML programming

Started by mikeebean, May 09, 2007, 02:49 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

mikeebean

I'm trying to learn ML programming, and I'm using VICE 128 as it has the built-in monitor. I understand there are a lot of complications involved with having ML programs at $4000 and up. Since I'm just learning and can't quite grasp banking and how it works yet, I'm putting my programs at $3000 and $2000.

Thing is, when I execute the programs, they tend to restart the computer. I'm using Jim Butterfield's beginner's ML book. The second program JSR's to BSOUT to output a character to the screen. I thought that by having my program below $4000 I would have access to Kernal calls? Or is it something else? (When I SYS to my program from BASIC, it works fine.) Here's my program:

3000  LDA #$48
3002  JSR $FFD2
3005  BRK

Any advice would be greatly appreciated!

bacon

You don't say how you start the program from the monitor. If you do a G 3000 it will execute in bank 0 IIRC, which is all RAM0 and you won't have access to the KERNAL. Try G F3000 (bank 15, which is the normal BASIC + KERNAL + I/O bank) and see what happens.
Bacon
-------------------------------------------------------
Das rubbernecken Sichtseeren keepen das cotton-pickenen Hands in die Pockets muss; relaxen und watschen die Blinkenlichten.

istvan

The 128 memory scheme can be confusing at first, but you'll have to get a handle on it before you can really get anywhere with ML.  On the 128, nearly every ML program you write should begin by setting the MMU configuration register, which is at $FF00.  Here's how this register works:

     bits 7-6:  ram bank
          00 for bank 0 RAM
          01 for bank 1 RAM
          10 for bank 2 RAM (if you have 256k)
          11 for bank 3 RAM (if you have 256k)

     bits 5-4:  contents of $C000-$FFFF
          00 for KERNAL ROM
          01 for internal function ROM
          10 for external function ROM
          11 for RAM

     bits 3-2:  contents of $8000-BFFF
          00 for BASIC HI ROM
          01 for internal function ROM
          10 for external function ROM
          11 for RAM

     bit 1: contents of $4000-7FFF
          0 for BASIC LO ROM
          1 for RAM

     bit 0: contents of $D000-DFFF
          0 for I/O registers
          1 for RAM or character ROM

So, say your program is at $2000;  we'll just stick with bank 0 ram, so bits 7-6 of the MMU config will be 00.  You want to use a kernal routine, so to enable the kernal, bits 5-4 will be 00.  Since we're not using BASIC for anything, let's get rid of it; bits 3-2 will be 11 and bit 1 will be 1, which gives RAM from $4000-BFFF.  We're not messing with the character definitions, so let's set bit 0 to 0.  So, our MMU configuration will be %00001110 = $0E.  Make the first two instructions of your program:

LDA #$0E
STA $FF00

Voila.  After those two instructions, the processor "sees" bank 0 RAM from $0000 to $BFFF, the kernal from $C000-FFFF, and hardware registers from $D000-DFFF.  There's no iron-clad way to know how the MMU will be setup when you start executing your ML program, so it's best to always set it explicitly.  The processor doesn't know or care what's at $FFD2 when you JSR there, it just goes and starts executing what it sees.  If the kernal isn't "visible" in the current memory configuration, you'll be JSRing into RAM, and executing whatever values happen to be stored there, with unpredictable (and usually messy) results.

bacon

When just starting out learning ML, it might be a good thing not to have to worry about learning the MMU registers and all that. Just specifying what bank you want to run the program in when starting it from the monitor works very well for the short programs in Jim Butterfield's book.

Btw, I tried my own advice in VICE and it works as expected.
Bacon
-------------------------------------------------------
Das rubbernecken Sichtseeren keepen das cotton-pickenen Hands in die Pockets muss; relaxen und watschen die Blinkenlichten.

mikeebean

Thank you guys, this is so much help! It actually makes a lot more sense the way you presented it than the way the Programmer's Reference Guide does. I find myself scratching my head even after reading parts of the P.R.G. two or three times, then I check the internet and find a much clearer example. I should probably look around for Jim Butterfield's ML book that includes the 128. Besides, I like to have as much information at my disposal as possible, even if I never use it.

Yes, I was using G 3000 from inside the monitor, no need for BASIC at this point. I also found a short two line routine in an article from Compute Magazine on atarimagazines.com similar to what istvan described above, but it was referring to using memoru at $4000 or above, so I figured it wasn't necessary at $3FFF or below. Maybe the 128 isn't the best place to start, but I also figure it forces me to learn this stuff right off the bat!

By the way istvan, love your icon, it makes me think of Futurama! Thanks you guys!