Difference between revisions of "Source Codes"
From CPCWiki - THE Amstrad CPC encyclopedia!
m (→CPC Plus: - removed "Programming:" in link description) |
m (Add link to 'A Survey of Advanced Assembly Language Programming Techniques on the MOS 6502 and Zilog Z80') |
||
(49 intermediate revisions by 13 users not shown) | |||
Line 2: | Line 2: | ||
''This article contains source codes and programming examples. You may also have a look at'' | ''This article contains source codes and programming examples. You may also have a look at'' | ||
* [[Programming software]] | * [[Programming software]] | ||
+ | * [[BIOS Functions]] | ||
* [[Technical documentation]]s | * [[Technical documentation]]s | ||
* [[Locomotive BASIC]] | * [[Locomotive BASIC]] | ||
Line 10: | Line 11: | ||
*[[Z80_-_undocumented_opcodes|Z80 - undocumented opcodes]] | *[[Z80_-_undocumented_opcodes|Z80 - undocumented opcodes]] | ||
*[[Where to learn?]] | *[[Where to learn?]] | ||
+ | *[[A little optimization for Z80 Assembler]] | ||
+ | *[https://github.com/sebastiengoutal/publications/blob/cae9abe5d684f98912571e4e713fc7af5f70760c/A%20Survey%20of%20Advanced%20Assembly%20Language%20Programming%20Techniques%20on%20the%20MOS%206502%20and%20Zilog%20Z80.pdf A publication on advanced assembly programming techniques on the Z80] | ||
== Algorithms == | == Algorithms == | ||
+ | *[[Programming:Bubble sort|Bubble sort]] | ||
*[[Programming:CPC OS floating point routines|CPC OS floating point routines]] | *[[Programming:CPC OS floating point routines|CPC OS floating point routines]] | ||
*[[Programming:CRC16|CRC16]] | *[[Programming:CRC16|CRC16]] | ||
Line 17: | Line 21: | ||
*[[Programming:Integer Division|Integer Division]] | *[[Programming:Integer Division|Integer Division]] | ||
*[[Programming:Integer Multiplication|Integer Multiplication]] | *[[Programming:Integer Multiplication|Integer Multiplication]] | ||
+ | *[[Programming:Logarithm|Logarithm]] | ||
*[[Programming:Ultrafast Multiplication|Ultrafast Multiplication]] by [[Prodatron]] | *[[Programming:Ultrafast Multiplication|Ultrafast Multiplication]] by [[Prodatron]] | ||
*[[Programming:Fast Square Root|Fast Square Root]] | *[[Programming:Fast Square Root|Fast Square Root]] | ||
Line 23: | Line 28: | ||
*[[Programming:Square Root|Square Root]] | *[[Programming:Square Root|Square Root]] | ||
*[[Programming:Precalculated square|Precalculated square]] | *[[Programming:Precalculated square|Precalculated square]] | ||
+ | *[[Programming:Maze generation|Maze generation]] | ||
*[[Programming:Random Number Generator|Random Number Generator]] | *[[Programming:Random Number Generator|Random Number Generator]] | ||
+ | *[[Programming:Reverse A|Reverse A]] | ||
+ | *[[Programming:Quicksort|Quicksort]] | ||
*[[Programming:Sin/Cos calculation|Sin/Cos calculation]] | *[[Programming:Sin/Cos calculation|Sin/Cos calculation]] | ||
== CP/M == | == CP/M == | ||
*[[Programming:A simple 'Hello World' program for CP/M using BDOS|A simple 'Hello World' program for CP/M using BDOS]] | *[[Programming:A simple 'Hello World' program for CP/M using BDOS|A simple 'Hello World' program for CP/M using BDOS]] | ||
+ | *[[Programming:A simple 'Hello World' program for CP/M using BIOS|A simple 'Hello World' program for CP/M using BIOS]] | ||
*[[Programming:Executing firmware functions from within CP/M 2.1|Executing firmware functions from within CP/M 2.1]] | *[[Programming:Executing firmware functions from within CP/M 2.1|Executing firmware functions from within CP/M 2.1]] | ||
*[[Programming:Executing firmware functions from within CP/M 2.1 or CP/M plus|Executing firmware functions from within CP/M 2.1 or CP/M plus]] | *[[Programming:Executing firmware functions from within CP/M 2.1 or CP/M plus|Executing firmware functions from within CP/M 2.1 or CP/M plus]] | ||
Line 34: | Line 43: | ||
== CPC Plus == | == CPC Plus == | ||
+ | *[[Amstrad CPC plus sprite format]] | ||
+ | *[[Programming:Convert CPC sprites to Plus hardware sprites|Convert CPC sprites to Plus hardware sprites]] | ||
+ | *[[Sprites Multiplexing]] | ||
*[[Programming:CPC Plus Hardware Sprites|Hardware sprites]] | *[[Programming:CPC Plus Hardware Sprites|Hardware sprites]] | ||
*[[Programming:CPC Plus Horizontal scroll|Horizontal scroll]] | *[[Programming:CPC Plus Horizontal scroll|Horizontal scroll]] | ||
*[[Programming:CPC Plus RLE Hardware Sprites|RLE hardware sprites]] | *[[Programming:CPC Plus RLE Hardware Sprites|RLE hardware sprites]] | ||
*[[Programming:CPC Plus Screen Splitting|Screen splitting]] | *[[Programming:CPC Plus Screen Splitting|Screen splitting]] | ||
− | |||
*[[Programming:CPC Plus Vertical scroll|Vertical scroll]] | *[[Programming:CPC Plus Vertical scroll|Vertical scroll]] | ||
+ | *[[Operation of Z80 interrupt mode 0 in the CPC plus design]] | ||
*[[Programming:Simple Raster Example 2 (uses CPC plus features)|Simple Raster Example 1 (uses CPC plus features)]] | *[[Programming:Simple Raster Example 2 (uses CPC plus features)|Simple Raster Example 1 (uses CPC plus features)]] | ||
*[[Programming:Simple Raster Example 3 (uses CPC plus features)|Simple Raster Example 2 (uses CPC plus features)]] | *[[Programming:Simple Raster Example 3 (uses CPC plus features)|Simple Raster Example 2 (uses CPC plus features)]] | ||
+ | *[[Programming:Unlocking ASIC|Unlocking ASIC]] | ||
== Devices == | == Devices == | ||
Line 52: | Line 65: | ||
*[[Programming:An example to read a file byte-by-byte|An example to read a file byte-by-byte]] | *[[Programming:An example to read a file byte-by-byte|An example to read a file byte-by-byte]] | ||
*[[Programming:An example to write a file byte-by-byte|An example to write a file byte-by-byte]] | *[[Programming:An example to write a file byte-by-byte|An example to write a file byte-by-byte]] | ||
+ | *[[Programming:Read/Write a file quickly byte-by-byte|Read or write a file quickly byte-by-byte]] | ||
*[[Programming:A simple file copier using firmware functions (copies byte-by-byte)|A simple file copier using firmware functions (copies byte-by-byte)]] | *[[Programming:A simple file copier using firmware functions (copies byte-by-byte)|A simple file copier using firmware functions (copies byte-by-byte)]] | ||
*[[Programming:Loading a file|Loading a file]] | *[[Programming:Loading a file|Loading a file]] | ||
*[[Programming:Saving a file|Saving a file]] | *[[Programming:Saving a file|Saving a file]] | ||
+ | *[[Programming:Unlocking a protected basic file|Unlocking a protected basic file]] | ||
+ | *[[Programming:Undo delete of file|Undo delete of file]] | ||
== Floppy disk == | == Floppy disk == | ||
Line 60: | Line 76: | ||
*[[Programming:A simple disc formatter using BDOS functions|A simple disc formatter using BDOS functions]] | *[[Programming:A simple disc formatter using BDOS functions|A simple disc formatter using BDOS functions]] | ||
*[[Programming:An example loader|An example loader]] | *[[Programming:An example loader|An example loader]] | ||
+ | *[[Programming:Catalog a disc and retrieve a directory|Catalog a disc and retrieve a directory]] | ||
*[[Programming:Detecting an Amstrad or Vortex disc controler|Detecting an Amstrad or Vortex disc controler]] | *[[Programming:Detecting an Amstrad or Vortex disc controler|Detecting an Amstrad or Vortex disc controler]] | ||
*[[Programming:Formatting a track on a disc|Formatting a track on a disc]] | *[[Programming:Formatting a track on a disc|Formatting a track on a disc]] | ||
*[[Programming:Reading a sector from a disc|Reading a sector from a disc]] | *[[Programming:Reading a sector from a disc|Reading a sector from a disc]] | ||
+ | *[[Programming:Reading and writing the boot sector of a SYSTEM/VENDOR disc|Reading and writing the boot sector of a SYSTEM/VENDOR disc]] | ||
*[[Programming:Writing a sector to disc|Writing a sector to disc]] | *[[Programming:Writing a sector to disc|Writing a sector to disc]] | ||
+ | |||
+ | == Games == | ||
+ | *[[Programming:Coding a simple BASIC game into Assembly]] | ||
== Graphics == | == Graphics == | ||
+ | |||
+ | *[[Programming:Calculating xpos,ypos to screen address in assembly|Calculating xpos,ypos to screen address]] | ||
*[[Programming:Display a 8-bit number in binary|Display a 8-bit number in binary]] | *[[Programming:Display a 8-bit number in binary|Display a 8-bit number in binary]] | ||
*[[Programming:Display a 8-bit number in hex|Display a 8-bit number in hex]] | *[[Programming:Display a 8-bit number in hex|Display a 8-bit number in hex]] | ||
Line 74: | Line 97: | ||
*[[Programming:Fast Sprites|Fast Sprites]] by [[User:Executioner|Executioner]] | *[[Programming:Fast Sprites|Fast Sprites]] by [[User:Executioner|Executioner]] | ||
*[[Programming:Fast Textoutput|Fast Textoutput]] by [[Prodatron]] | *[[Programming:Fast Textoutput|Fast Textoutput]] by [[Prodatron]] | ||
+ | *[[Fastest_Character_Print_in_Mode_2|Fastest Character Print in Mode 2 in all screen positions]] by [[FG Brain]] | ||
*[[Programming:Hardware scrolling|Hardware Scrolling]] by [[User:Executioner|Executioner]] | *[[Programming:Hardware scrolling|Hardware Scrolling]] by [[User:Executioner|Executioner]] | ||
*[[Programming:Hardware scrolling 2|Hardware Scrolling 2]] | *[[Programming:Hardware scrolling 2|Hardware Scrolling 2]] | ||
*[[Programming:Hardware scrolling the screen horizontally byte-by-byte using the CRTC|Hardware scrolling the screen horizontally byte-by-byte using the CRTC]] | *[[Programming:Hardware scrolling the screen horizontally byte-by-byte using the CRTC|Hardware scrolling the screen horizontally byte-by-byte using the CRTC]] | ||
*[[Programming:Hardware scrolling the screen using the CRTC|Hardware scrolling the screen using the CRTC]] | *[[Programming:Hardware scrolling the screen using the CRTC|Hardware scrolling the screen using the CRTC]] | ||
+ | *[[Programming:Next / previous line calculation|Next / previous line calculation]] | ||
*[[Programming:Overscan|Overscan]] | *[[Programming:Overscan|Overscan]] | ||
*[[Programming:Plotting a sprite using character matrices|Plotting a sprite using character matrices]] | *[[Programming:Plotting a sprite using character matrices|Plotting a sprite using character matrices]] | ||
+ | *[[Mode 4 (two bitplane emulation)| 4th Mode of CPCs: Plotting sprites on 2 bitplanes]] | ||
+ | *[[Screen refresh rate|Set the screen refresh rate]] | ||
*[[Programming:Simple Raster Example 1|Simple Raster Example]] | *[[Programming:Simple Raster Example 1|Simple Raster Example]] | ||
*[[Programming:Simple Split Raster Example 1|Simple Split Raster Example]] | *[[Programming:Simple Split Raster Example 1|Simple Split Raster Example]] | ||
*[[Synchronising with the CRTC and display]] | *[[Synchronising with the CRTC and display]] | ||
+ | *[[CRTC change colour (fill) test with precise timing]] by [[User:Matahari|Matahari]] | ||
+ | *[[256 byte Overscan MEGATEXT Intro - (features 50Hz fullscreen scroll)]] by [[User:Matahari|Matahari]] | ||
== Interrupts == | == Interrupts == | ||
Line 89: | Line 118: | ||
== Keyboard == | == Keyboard == | ||
*[[Programming:Keyboard scanning|Keyboard scanning]] | *[[Programming:Keyboard scanning|Keyboard scanning]] | ||
+ | *[[Programming:Keyboard_redefinition|Keyboard redefinition]] | ||
+ | |||
+ | == Cartridges == | ||
+ | *[[Programming:Cartridges|Cartridges]] | ||
+ | *[[Programming:Crt0Cart|CRT0 for SDCC]] to init a CPR cart | ||
== Other routines == | == Other routines == | ||
Line 96: | Line 130: | ||
*[[Programming:Dumping the data of BASIC or AMSDOS or an expansion rom|Dumping the data of BASIC or AMSDOS or an expansion rom]] | *[[Programming:Dumping the data of BASIC or AMSDOS or an expansion rom|Dumping the data of BASIC or AMSDOS or an expansion rom]] | ||
*[[Programming:Dumping the data of the lower rom|Dumping the data of the lower rom]] | *[[Programming:Dumping the data of the lower rom|Dumping the data of the lower rom]] | ||
+ | *[[Programming:Storing and Retrieving Screens from the extra 64kb|Storing and Retrieving Screens from the extra 64kb]] | ||
+ | |||
+ | == Sound == | ||
+ | *[[How to access the PSG via PPI]] | ||
+ | *[[Programming:Tutorial - Understanding the fundamentals of BASIC SOUND and the Firmware SOUND QUEUE|Tutorial - Understanding the fundamentals of BASIC SOUND and the Firmware SOUND QUEUE]] | ||
+ | *[[Programming:Source code to show 0x0ff is always returned when reading PSG port B|Source code to show 0x0ff is always returned when reading PSG port B]] | ||
+ | *[[Programming:Source code to show it is possible to store data in PSG register 14 and 15 (port A and port B)|Source code to show it is possible to store data in PSG register 14 and 15 (port A and port B)]] | ||
+ | *[[Programming:Source code to show it is possible to store data in PSG register 14 and 15 even if the port has been set to input|Source code to show it is possible to store data in PSG register 14 and 15 even if the port has been set to input]] | ||
+ | *[[Programming:Source code to show that some registers always return 0 in some bits|Source code to show that some registers always return 0 in some bits]] | ||
+ | *[[Programming:Source code to show that when a port is read in output mode; the data read will be ANDed with the inputs to that port|Source code to show that when a port is read in output mode; the data read will be ANDed with the inputs to that port]] | ||
== Cross Development == | == Cross Development == | ||
*[[Programming:Cross Development|Cross Development]] | *[[Programming:Cross Development|Cross Development]] | ||
+ | |||
+ | [[Category:Source code]] | ||
+ | |||
+ | == General Notes == | ||
+ | |||
+ | * Memory range for programs is &0040-&a700. This avoids firmware and memory allocated by AMSDOS disc ROM. | ||
+ | This is safe for the purpose of loading and being compatible with the firmware. | ||
+ | After loading, if you disable the firmware, you can re-use the firmware areas as you want, but you need to do everything yourself (scanning keyboard, drawing, sound etc).If you need to use these areas, a common thing to do is to load most into the safe area, some into the screen, and relocate it after loading. | ||
+ | * Programs on cassette and disc have a header that define the load address, length and execution address. | ||
+ | |||
+ | In Pasmo assembler use the "end" mnemonic to define the label which is the execution address and use "--amsdos" to put an AMSDOS header on it. | ||
+ | |||
+ | * Basic programs start at &170. | ||
+ | * Firmware uses interrupt mode 1 of the Z80 (interrupts jump to &0038) | ||
+ | * Lowest place you can LOAD a binary file to with BASIC is &389 e.g.: | ||
+ | |||
+ | openout"d" | ||
+ | memory <address>-1 | ||
+ | closeout | ||
+ | load "code",<address> | ||
+ | call <exec> | ||
+ | |||
+ | *Screen is normally at &c000-&ffff. (It can be changed using firmware, or using CRTC R12/R13) | ||
+ | *Stack is normally at &c000 and goes down. | ||
+ | *Extra registers (BC', AF', HL', DE' are reserved by the firmware). Avoid if you are using firmware functions. | ||
+ | *Lower rom (containing OS) can be paged into memory between &0000-&3fff. | ||
+ | *Upper rom is selectable, examples are BASIC and AMSDOS. They can be paged into memory between &c000-&ffff. | ||
+ | *From basic, a game is run with: | ||
+ | |||
+ | RUN"<filename> | ||
+ | |||
+ | keep it in the safe memory ranges and it'll run from cassette and disc. | ||
+ | *Screen is normally 40 crtc chars wide (CRTC R1=40), 25 crtc chars tall (CRTC R6=25). 8 scan lines per char (R9=7). Firmware functions assume this. | ||
+ | *Screen is bitmapped. You must draw/erase your own sprites and text. | ||
+ | *Firmware refreshes the palette every 50th of a second, so you need to turn off the firmware and use the hardware directly, or set the colours using firmware. | ||
+ | *Firmware can be "turned off", by disabling lower ROM, redirecting interrupts and not calling firmware functions. | ||
+ | *Screen can be resized the same as the Spectrum. Provided you do not use hardware scrolling, this gives you extra space although because of the layout of the screen, it's not continuous, it's in 8 seperate blocks, but it's enough to store data and code. |
Latest revision as of 14:55, 28 October 2023
This article contains source codes and programming examples. You may also have a look at
Contents
Assembler
- Z80 - undocumented opcodes
- Where to learn?
- A little optimization for Z80 Assembler
- A publication on advanced assembly programming techniques on the Z80
Algorithms
- Bubble sort
- CPC OS floating point routines
- CRC16
- CRC32
- Integer Division
- Integer Multiplication
- Logarithm
- Ultrafast Multiplication by Prodatron
- Fast Square Root
- Fast 16 bit Square Root by Executioner
- Filling memory with a byte
- Square Root
- Precalculated square
- Maze generation
- Random Number Generator
- Reverse A
- Quicksort
- Sin/Cos calculation
CP/M
- A simple 'Hello World' program for CP/M using BDOS
- A simple 'Hello World' program for CP/M using BIOS
- Executing firmware functions from within CP/M 2.1
- Executing firmware functions from within CP/M 2.1 or CP/M plus
- Executing firmware functions from within CP/M 2.1
- Executing firmware functions from within CP/M plus
CPC Plus
- Amstrad CPC plus sprite format
- Convert CPC sprites to Plus hardware sprites
- Sprites Multiplexing
- Hardware sprites
- Horizontal scroll
- RLE hardware sprites
- Screen splitting
- Vertical scroll
- Operation of Z80 interrupt mode 0 in the CPC plus design
- Simple Raster Example 1 (uses CPC plus features)
- Simple Raster Example 2 (uses CPC plus features)
- Unlocking ASIC
Devices
File access
- An example to read a file byte-by-byte
- An example to write a file byte-by-byte
- Read or write a file quickly byte-by-byte
- A simple file copier using firmware functions (copies byte-by-byte)
- Loading a file
- Saving a file
- Unlocking a protected basic file
- Undo delete of file
Floppy disk
- A simple disc copier using BDOS functions
- A simple disc formatter using BDOS functions
- An example loader
- Catalog a disc and retrieve a directory
- Detecting an Amstrad or Vortex disc controler
- Formatting a track on a disc
- Reading a sector from a disc
- Reading and writing the boot sector of a SYSTEM/VENDOR disc
- Writing a sector to disc
Games
Graphics
- Calculating xpos,ypos to screen address
- Display a 8-bit number in binary
- Display a 8-bit number in hex
- Display a byte as a 3-digit decimal number
- Display and update Scores
- Distorting the screen using register 2 of the CRTC (Horizontal Sync Position)
- Fast plot
- Fast Sprites by Executioner
- Fast Textoutput by Prodatron
- Fastest Character Print in Mode 2 in all screen positions by FG Brain
- Hardware Scrolling by Executioner
- Hardware Scrolling 2
- Hardware scrolling the screen horizontally byte-by-byte using the CRTC
- Hardware scrolling the screen using the CRTC
- Next / previous line calculation
- Overscan
- Plotting a sprite using character matrices
- 4th Mode of CPCs: Plotting sprites on 2 bitplanes
- Set the screen refresh rate
- Simple Raster Example
- Simple Split Raster Example
- Synchronising with the CRTC and display
- CRTC change colour (fill) test with precise timing by Matahari
- 256 byte Overscan MEGATEXT Intro - (features 50Hz fullscreen scroll) by Matahari
Interrupts
Keyboard
Cartridges
- Cartridges
- CRT0 for SDCC to init a CPR cart
Other routines
- An example boot sector (executed with rsx command CPM)
- An example to define a RSX
- Calling a RSX from outside of BASIC
- Dumping the data of BASIC or AMSDOS or an expansion rom
- Dumping the data of the lower rom
- Storing and Retrieving Screens from the extra 64kb
Sound
- How to access the PSG via PPI
- Tutorial - Understanding the fundamentals of BASIC SOUND and the Firmware SOUND QUEUE
- Source code to show 0x0ff is always returned when reading PSG port B
- Source code to show it is possible to store data in PSG register 14 and 15 (port A and port B)
- Source code to show it is possible to store data in PSG register 14 and 15 even if the port has been set to input
- Source code to show that some registers always return 0 in some bits
- Source code to show that when a port is read in output mode; the data read will be ANDed with the inputs to that port
Cross Development
General Notes
- Memory range for programs is &0040-&a700. This avoids firmware and memory allocated by AMSDOS disc ROM.
This is safe for the purpose of loading and being compatible with the firmware. After loading, if you disable the firmware, you can re-use the firmware areas as you want, but you need to do everything yourself (scanning keyboard, drawing, sound etc).If you need to use these areas, a common thing to do is to load most into the safe area, some into the screen, and relocate it after loading.
- Programs on cassette and disc have a header that define the load address, length and execution address.
In Pasmo assembler use the "end" mnemonic to define the label which is the execution address and use "--amsdos" to put an AMSDOS header on it.
- Basic programs start at &170.
- Firmware uses interrupt mode 1 of the Z80 (interrupts jump to &0038)
- Lowest place you can LOAD a binary file to with BASIC is &389 e.g.:
openout"d" memory <address>-1 closeout load "code",<address> call <exec>
- Screen is normally at &c000-&ffff. (It can be changed using firmware, or using CRTC R12/R13)
- Stack is normally at &c000 and goes down.
- Extra registers (BC', AF', HL', DE' are reserved by the firmware). Avoid if you are using firmware functions.
- Lower rom (containing OS) can be paged into memory between &0000-&3fff.
- Upper rom is selectable, examples are BASIC and AMSDOS. They can be paged into memory between &c000-&ffff.
- From basic, a game is run with:
RUN"<filename>
keep it in the safe memory ranges and it'll run from cassette and disc.
- Screen is normally 40 crtc chars wide (CRTC R1=40), 25 crtc chars tall (CRTC R6=25). 8 scan lines per char (R9=7). Firmware functions assume this.
- Screen is bitmapped. You must draw/erase your own sprites and text.
- Firmware refreshes the palette every 50th of a second, so you need to turn off the firmware and use the hardware directly, or set the colours using firmware.
- Firmware can be "turned off", by disabling lower ROM, redirecting interrupts and not calling firmware functions.
- Screen can be resized the same as the Spectrum. Provided you do not use hardware scrolling, this gives you extra space although because of the layout of the screen, it's not continuous, it's in 8 seperate blocks, but it's enough to store data and code.