2018-08-06 01:35:43 +00:00
|
|
|
***************************
|
|
|
|
Target system specification
|
|
|
|
***************************
|
|
|
|
|
2018-09-15 14:21:05 +00:00
|
|
|
Prog8 targets the following hardware:
|
2018-08-06 01:35:43 +00:00
|
|
|
|
2020-08-27 16:18:29 +00:00
|
|
|
- 8 bit MOS 6502/65c02/6510 CPU
|
2018-08-06 01:35:43 +00:00
|
|
|
- 64 Kb addressable memory (RAM or ROM)
|
2020-08-27 16:18:29 +00:00
|
|
|
- optional use of memory mapped I/O registers
|
|
|
|
- optional use of system ROM routines
|
2018-08-06 01:35:43 +00:00
|
|
|
|
2020-09-20 21:49:36 +00:00
|
|
|
Currently there are two machines that are supported as compiler target (selectable via the ``-target`` compiler argument):
|
2020-08-27 16:18:29 +00:00
|
|
|
|
2020-08-30 16:32:16 +00:00
|
|
|
- 'c64': the well-known Commodore-64, premium support
|
|
|
|
- 'cx16': the `CommanderX16 <https://www.commanderx16.com/>`_ a project from the 8-Bit Guy. Support for this is still experimental.
|
|
|
|
|
|
|
|
This chapter explains the relevant system details of these machines.
|
2018-08-06 01:35:43 +00:00
|
|
|
|
2020-10-17 18:35:36 +00:00
|
|
|
.. hint::
|
|
|
|
If you only use standard kernel and prog8 library routines,
|
|
|
|
it is possible to compile the *exact same program* for both machines (just change the compiler target flag)!
|
2020-09-21 22:47:02 +00:00
|
|
|
|
2018-08-06 01:35:43 +00:00
|
|
|
|
|
|
|
Memory Model
|
|
|
|
============
|
|
|
|
|
|
|
|
Physical address space layout
|
|
|
|
-----------------------------
|
|
|
|
|
|
|
|
The 6502 CPU can address 64 kilobyte of memory.
|
2018-09-15 14:21:05 +00:00
|
|
|
Most of the 64 kilobyte address space can be used by Prog8 programs.
|
2018-08-06 01:35:43 +00:00
|
|
|
This is a hard limit: there is no built-in support for RAM expansions or bank switching.
|
|
|
|
|
|
|
|
|
|
|
|
====================== ================== ========
|
|
|
|
memory area type note
|
|
|
|
====================== ================== ========
|
|
|
|
``$00``--``$ff`` ZeroPage contains many sensitive system variables
|
|
|
|
``$100``--``$1ff`` Hardware stack used by the CPU, normally not accessed directly
|
|
|
|
``$0200``--``$ffff`` Free RAM or ROM free to use memory area, often a mix of RAM and ROM
|
|
|
|
====================== ================== ========
|
|
|
|
|
|
|
|
|
|
|
|
A few of these memory addresses are reserved and cannot be used for arbitrary data.
|
|
|
|
They have a special hardware function, or are reserved for internal use in the
|
|
|
|
code generated by the compiler:
|
|
|
|
|
|
|
|
================== =======================
|
|
|
|
reserved address in use for
|
|
|
|
================== =======================
|
|
|
|
``$00`` data direction (CPU hw)
|
|
|
|
``$01`` bank select (CPU hw)
|
2018-09-15 14:21:05 +00:00
|
|
|
``$02`` internal scratch variable
|
|
|
|
``$03`` internal scratch variable
|
|
|
|
``$fb - $fc`` internal scratch variable
|
|
|
|
``$fd - $fe`` internal scratch variable
|
2018-08-06 01:35:43 +00:00
|
|
|
``$fffa - $fffb`` NMI vector (CPU hw)
|
|
|
|
``$fffc - $fffd`` RESET vector (CPU hw)
|
|
|
|
``$fffe - $ffff`` IRQ vector (CPU hw)
|
|
|
|
================== =======================
|
|
|
|
|
|
|
|
The actual machine will often have many other special addresses as well,
|
|
|
|
For example, the Commodore-64 has:
|
|
|
|
|
|
|
|
- ROMs installed in the machine: BASIC, kernal and character roms. Occupying ``$a000``--``$bfff`` and ``$e000``--``$ffff``.
|
|
|
|
- memory-mapped I/O registers, for the video and sound chips, and the CIA's. Occupying ``$d000``--``$dfff``.
|
|
|
|
- RAM areas that are used for screen graphics and sprite data: usually at ``$0400``--``$07ff``.
|
|
|
|
|
2018-09-15 14:21:05 +00:00
|
|
|
Prog8 programs can access all of those special memory locations but it will have a special meaning.
|
2018-08-06 01:35:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
.. _zeropage:
|
|
|
|
|
|
|
|
ZeroPage ("ZP")
|
|
|
|
---------------
|
|
|
|
|
|
|
|
The ZeroPage memory block ``$02``--``$ff`` can be regarded as 254 CPU 'registers', because
|
|
|
|
they take less clock cycles to access and need fewer instruction bytes than accessing other memory locations outside of the ZP.
|
|
|
|
Theoretically they can all be used in a program, with the follwoing limitations:
|
|
|
|
|
|
|
|
- several addresses (``$02``, ``$03``, ``$fb - $fc``, ``$fd - $fe``) are reserved for internal use
|
|
|
|
- most other addresses will already be in use by the machine's operating system or kernal,
|
|
|
|
and overwriting them will probably crash the machine. It is possible to use all of these
|
|
|
|
yourself, but only if the program takes over the entire system (and seizes control from the regular kernal).
|
|
|
|
This means it can no longer use (most) BASIC and kernal routines from ROM.
|
2018-09-15 14:21:05 +00:00
|
|
|
- it's more convenient and safe to let the compiler allocate these addresses for you and just
|
2018-08-06 01:35:43 +00:00
|
|
|
use symbolic names in the program code.
|
|
|
|
|
2018-09-15 14:21:05 +00:00
|
|
|
Prog8 knows what addresses are safe to use in the various ZP handling configurations.
|
2018-09-06 19:13:49 +00:00
|
|
|
It will use the free ZP addresses to place its ZP variables in,
|
2018-08-06 01:35:43 +00:00
|
|
|
until they're all used up. If instructed to output a program that takes over the entire
|
|
|
|
machine, (almost) all of the ZP addresses are suddenly available and will be used.
|
2018-09-06 19:13:49 +00:00
|
|
|
|
|
|
|
**ZeroPage handling is configurable:**
|
|
|
|
There's a global program directive to specify the way the compiler
|
|
|
|
treats the ZP for the program. The default is to be reasonably restrictive to use the
|
|
|
|
part of the ZP that is not used by the C64's kernal routines.
|
|
|
|
It's possible to claim the whole ZP as well (by disabling the operating system or kernal).
|
|
|
|
If you want, it's also possible to be more restricive and stay clear of the addresses used by BASIC routines too.
|
2019-02-02 23:14:56 +00:00
|
|
|
This allows the program to exit cleanly back to a BASIC ready prompt - something that is not possible in the other modes.
|
2018-08-06 01:35:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
IRQs and the ZeroPage
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
The normal IRQ routine in the C-64's kernal will read and write several addresses in the ZP
|
|
|
|
(such as the system's software jiffy clock which sits in ``$a0 - $a2``):
|
|
|
|
|
|
|
|
``$a0 - $a2``; ``$91``; ``$c0``; ``$c5``; ``$cb``; ``$f5 - $f6``
|
|
|
|
|
2018-09-06 19:13:49 +00:00
|
|
|
These addresses will *never* be used by the compiler for ZP variables, so variables will
|
2018-08-06 01:35:43 +00:00
|
|
|
not interfere with the IRQ routine and vice versa. This is true for the normal ZP mode but also
|
|
|
|
for the mode where the whole system and ZP have been taken over.
|
|
|
|
So the normal IRQ vector can still run and will be when the program is started!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CPU
|
|
|
|
===
|
|
|
|
|
|
|
|
Directly Usable Registers
|
|
|
|
-------------------------
|
|
|
|
|
2020-07-24 20:57:19 +00:00
|
|
|
The hardware CPU registers are not directly accessible from regular Prog8 code.
|
|
|
|
If you need to mess with them, you'll have to use inline assembly.
|
|
|
|
Be extra wary of the ``X`` register because it is used as an evaluation stack pointer and
|
|
|
|
changing its value you will destroy the evaluation stack and likely crash the program.
|
2018-08-06 01:35:43 +00:00
|
|
|
|
2020-07-24 20:57:19 +00:00
|
|
|
The status register (P) carry flag and interrupt disable flag can be written via a couple of special
|
|
|
|
builtin functions (``set_carry()``, ``clear_carry()``, ``set_irqd()``, ``clear_irqd()``),
|
|
|
|
and read via the ``read_flags()`` function.
|
2019-01-24 01:43:25 +00:00
|
|
|
|
2018-09-24 20:34:12 +00:00
|
|
|
|
2018-08-06 01:35:43 +00:00
|
|
|
Subroutine Calling Conventions
|
|
|
|
------------------------------
|
|
|
|
|
2019-01-24 01:43:25 +00:00
|
|
|
**Kernel/assembly subroutines:**
|
2018-10-12 16:01:40 +00:00
|
|
|
Arguments and results are passed via registers.
|
2018-09-02 16:32:48 +00:00
|
|
|
Sometimes the status register's Carry flag is used as well (as a boolean flag).
|
2019-01-24 01:43:25 +00:00
|
|
|
Special care should be taken when the subroutine clobbers the X register.
|
|
|
|
If it does, X must be saved before and restored after the call.
|
2018-10-12 16:01:40 +00:00
|
|
|
|
|
|
|
**Normal user defined subroutines:**
|
2019-01-24 01:43:25 +00:00
|
|
|
Arguments and result values are passed via global variables stored in memory
|
|
|
|
*These are not allocated on a stack* so it is not possible to create recursive calls!
|
|
|
|
The result value(s) of a subroutine are returned on the evaluation stack,
|
|
|
|
to make it possible to use subroutines in expressions.
|
2019-03-06 21:11:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
IRQ Handling
|
|
|
|
============
|
|
|
|
|
|
|
|
Normally, the system's default IRQ handling is not interfered with.
|
|
|
|
You can however install your own IRQ handler.
|
|
|
|
This is possible ofcourse by doing it all using customized inline assembly,
|
|
|
|
but there are a few library routines available to make setting up C-64 IRQs and raster IRQs a lot easier (no assembly code required).
|
|
|
|
|
2020-08-27 16:18:29 +00:00
|
|
|
For the C64 these routines are::
|
2019-03-06 21:11:16 +00:00
|
|
|
|
2020-08-27 16:10:22 +00:00
|
|
|
c64.set_irqvec()
|
|
|
|
c64.set_irqvec_excl()
|
2019-03-06 21:11:16 +00:00
|
|
|
|
2020-08-27 16:10:22 +00:00
|
|
|
c64.set_rasterirq( <raster line> )
|
|
|
|
c64.set_rasterirq_excl( <raster line> )
|
2019-03-06 21:11:16 +00:00
|
|
|
|
2020-08-27 16:10:22 +00:00
|
|
|
c64.restore_irqvec() ; set it back to the systems default irq handler
|
2019-03-06 21:11:16 +00:00
|
|
|
|
|
|
|
If you activate an IRQ handler with one of these, it expects the handler to be defined
|
|
|
|
as a subroutine ``irq`` in the module ``irq`` so like this::
|
|
|
|
|
2019-07-29 21:11:13 +00:00
|
|
|
irq {
|
2019-03-06 21:11:16 +00:00
|
|
|
sub irq() {
|
|
|
|
; ... irq handling here ...
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 20:57:19 +00:00
|
|
|
|