10.2. Some quick examples

Before we go into detail with how segments work, let us take a look at some examples of use. You might not understand everything in the following examples, but it helps to know where we are heading before going into the details.

If you want to have one section of you code output to another file you can assemble it into a segment and write that segment to a file like this:

.segment File1 [outPrg="MyFile.prg"]
    lda #00
    ... more code ...
.segment Default

If you want to patch a file you can load the file into a Base segment, put a Patch segment on top of it with the modifications and write the result to a file. Since the Patch is on top it will overwrite the base:

.file [name="Out.prg", segments="Base,Patch", allowOverlap]
.segment Base [prgFiles="basefile.prg"]
.segment Patch [] 

    *=$8021 "Insert jump"
    jmp $8044

Segments can also be used for outputting code in alternative formats. Here is an example writing code for a cartridge with 4 banks:

.segment CARTRIDGE_FILE [outBin="myfile.bin"]
    .segmentout [segments ="BANK1"]
    .segmentout [segments ="BANK2"]
    .segmentout [segments ="BANK3"]
    .segmentout [segments ="BANK4"]

.segmentdef BANK1 [min=$1000, max=$1fff, fill]
.segmentdef BANK2 [min=$1000, max=$1fff, fill]
.segmentdef BANK3 [min=$1000, max=$1fff, fill]
.segmentdef BANK4 [min=$1000, max=$1fff, fill]

.segment BANK1
..code for segment 1 goes here...

.segment BANK2
..code for segment 2 goes here...

.segment BANK3
..code for segment 3 goes here...

.segment BANK4
..code for segment 4 goes here...

A segment is set up for each bank and they are output in the right order to a binary file. The code in the 4 segments is restricted to the address space $1000-$1fff. Notice how the same address space can be used multiple times, since the code resides in different segments.