Hi Fabien!
fabiensanglard wrote:
I see this a a really good exercice to increase my skills.
It sure is.
fabiensanglard wrote:
I am still split between trying to cross-compile or write something in C to assemble the source code into a bytecode with a virtual machine so the end result could run on a desktop machine.
Please allow me to recommend cross-assembling first. The program might use self-modifying code which will not easily translate to bytecode. You also would have to convert the graphics, too, which in itself is a hard thing to do. Since you'll find a decent AppleII emulator on all platforms today (a lot of them written in Java) I wouldn't worry about a virtual machine as the emulator already is a virtual machine.
fabiensanglard wrote:
I know next to nothing about Apple II or Apple //e
Oookay... Where do we start? .. <scratch head> ..
The memory of the AppleII is basically organized like this: ($.... = hexadecimal = 0x.... in C)
$0000 - $00ff: zero page
$0100 - $01ff: stack
$0200 - $02ff: used as an input buffer by Applesoft (Basic) otherwise free
$0300 - $03ff: vectors are put here
$0400 - $07ff: text and low-resolution graphics display buffer page 1
$0800 - $0bff: text and low-resolution graphics display buffer page 2
$0c00 - $1fff: free
$2000 - $3fff: high-resolution graphics display buffer page 1
$4000 - $5fff: high-resolution graphics display buffer page 2
$6000 - $bfff: free
$c000 - $cfff: IO (and additional ROM)
$d000 - $ffff: ROM or language card RAM (s.b.)
Note: low-resolution is usually abbreviated to lores, and high-resolution is called hires.
Note: if you don't use the text display or the hires graphics display this memory is also free as long as you do not use certain peripheral devices, since they put some data between $400 and $7ff (in ram that is not displayed on screen).
If you want to switch from text display to hires display you need what is called a 'softswitch.' This is a switch (flip-flop etc) that gets activated by simply accessing a memory location assigned to it. For example, there are two softswitches to choose between text or graphics:
LDA $c050 will select graphics
LDA $c051 will select text
Note: you can also write
BIT $c050
or (in this case) also
STA $c050
it doesn't matter.
If you want hires you write
LDA $c057
For lores you would do
LDA $c056
Full screen mode is selected by
LDA $c052
Mixed mode (4 bottom lines of text)
LDA $c053
At the beginning of file 'MASTER.S' you will see a list with the following elements:
Code:
HIRESon = $c057
HIRESoff = $c056
PAGE2on = $c055
PAGE2off = $c054
MIXEDon = $c053
MIXEDoff = $c052
TEXTon = $c051
TEXToff = $c050
You also spot the 'PAGE2on' and 'PAGE2off.' In addition to the display mode you can select a dedicated memory area for display. (If you know the C64 then this is sort of writing a value to $d018). There are only two possibilities:
$0400 - $07ff: text and low-resolution graphics display buffer page 1
$0800 - $0bff: text and low-resolution graphics display buffer page 2
and
$2000 - $3fff: high-resolution graphics display buffer page 1
$4000 - $5fff: high-resolution graphics display buffer page 2
What POP does is it selects page 1 and then writes data (shapes etc) to page 2. Then it switches to page 2 and writes to page 1. This is known as 'double buffering' and is used to reduce flicker. See the following example:
Code:
*-------------------------------
*
* Credit line disappears
*
*-------------------------------
CleanScreen
* Switch to DHires page 2
* (credit line disappears)
lda PAGE2on
* Copy DHires page 2 back to hidden page 1
jsr copy2to1
* Display page 1
lda PAGE2off
]rts rts
As seen before, the 6502 can only address memory from $0000 .. $ffff. This equals to 65536 bytes = 64 kilo bytes. Now the Apple//e (enhanced) and Apple//c came with more than 64kb. This was only possible using bank switching. This means that a certain area was assigned (at least) twice to different RAM/ROM chips. The original RAM chip (main memory) was found on the motherboard. while the additional 64kb were put in an extra chip and called auxiliary ram. This was necessary because the video generator could then access both chips simultaneously (2 bytes per clock cycle) to display the double high-resolution of 560x192. With only one chip the standard resolution (hires = 280x192) was restricted to one byte per clock cycle. The first cards that used this were video cards that could display 80 column text instead of 40. Here the same trick applies: two bytes are read, one from the text memory on the motherboard and one from the video card. The questions was how to integrate the additional ram into memory. The solution was simple (from a hardware designers point of view). First you would have to select a special bank switching mode: 80 column store. This is done by using
STA $c001
This time it only works with STA, not LDA or BIT. This is true for all softswitches between $c000 and $c00f.
STA $c000
will switch back to normal memory mode. During the 80 column store mode the softswitches $c054 and $c055 (page selector) do no longer select the display area but which memory the 6502 will see (read and write): either the memory on the motherboard (STA $c054) or the aux memory (STA $c055). If you select graphics display (STA $c050) and hires (STA $c057) then this will also apply to $2000..$3fff. Since the display selector is deactivated in this mode, text page 1 or hires page 1 will always be displayed (IIRC). For the first 80 column cards you only needed an additional 1kb. Later on this was expanded to a full 64kb. Since the 80 column mode only works with $400..$7ff and $2000..$3fff another method is needed to access the remaining memory.
STA $c003
does the trick. Almost. Using this softswitch you can access $200..$bfff of the aux ram for _read_ access.
STA $c002
will switch back to main ram.
STA $c005
will allow _write_ access to $200..$bfff of the aux ram
STA $c004
will switch back to main ram.
Now what happens when your program resides in main memory, say at address $6000, and you do
STA $c003
Your program will probably crash because the processor will also read the program data now from aux not main. Therefore $0000..$01ff are excluded because here you find the important zero page and also the stack. What you could do is, you could write a short program that resides in this area ($0000..$01ff) and loads from/stores to aux memory. Note: the 80column mode will override $c002.. So it is possible to access the aux mem at $6000 and the main mem at $400 at the same time. Best to think of these modes as separate windows into the memory that can overlap if wanted.
The area found at $d000 .. $ffff is special because you will find the ROM here as well as a ram extension known as language card (l.c. in POP). It was meant to be used as a replacement for the Integer Basic on old AppleIIs (e.g. with Applesoft Basic or Pascal) hence the name. To activate it you must select the lc card with softswitches between $c080 and $c08f)
LDA $c080 = read bank 2
LDA $c081 = (twice) write bank2, read rom 12
LDA $c082 = read rom 12
LDA $c083 = (twice) write/read bank 2
LDA $c088 = read bank 1
LDA $c089 = (twice) write bank1, read rom 12
LDA $c08a = read rom 12
LDA $c08b = (twice) write/read bank 1
twice means, well, doing it twice:
LDA $c081
LDA $c081
Since $d000..$ffff only offers room for 3kb but 4kb were wanted, the area $d000..$dfff was divided into two banks 1 and 2. That's what
RWBANK2 = $c083
RWBANK1 = $c08b
is for. See the example from the code:
Code:
* switch in bank 2
bit RWBANK2
bit RWBANK2 ;2nd 4k bank
This will select the second bank of the language card. Now take a look again at softswitches $c002..$c005. They will select the area $0200..$bfff. The remaining $0000..$01ff and $d000..$ffff can be selected using
STA $c008 = read/write main zp/stack + lc
STA $c009 = read/write aux zp/stack + lc
Note: with an additional 64kb there are in fact two lc cards. As long as your DOS resides in one of the language cards you can safely select the aux memory between $0200..$bfff. This is what POP does:
Code:
setaux sta RAMRDaux
sta RAMWRTaux
rts
setmain sta RAMRDmain
sta RAMWRTmain
rts
So much for now. HTH
Cheers
Miles