Skip to content

Commit

Permalink
Add C16 BASIC target, improve object placement and fix merging blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Jul 10, 2024
1 parent fc119b0 commit 86533d9
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 25 deletions.
5 changes: 5 additions & 0 deletions share/target/c16-basic.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.include "c16.inc"

basic_end = $3fff

.include "commodore-264-basic.inc"
6 changes: 6 additions & 0 deletions share/target/c16.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.cpu "6502"

.define C16

.include "commodore-264.inc"

13 changes: 13 additions & 0 deletions share/target/commodore-264-basic.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.section zero_page {
type: reserve_only
address [
: $03 - $06 ; Storage for RENUMBER.
: $19 - $21 ; Temporary string stack.
: $26 - $2a ; Floating point result of multiplication or division.
: $d8 - $e8 ; Unused.
]
}

basic_start = $1001

.include "commodore-basic.inc"
7 changes: 7 additions & 0 deletions share/target/commodore-264.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.cpu "6502"

.define COMMODORE_264

; TODO: Create own encodings file.
.include "c64-encodings.inc"

13 changes: 1 addition & 12 deletions share/target/plus4-basic.target
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
.include "plus4.inc"

.section zero_page {
type: reserve_only
address [
: $03 - $06 ; Storage for RENUMBER.
: $19 - $21 ; Temporary string stack.
: $26 - $2a ; Floating point result of multiplication or division.
: $d8 - $e8 ; Unused.
]
}

basic_start = $1001
basic_end = $7fff

.include "commodore-basic.inc"
.include "commodore-264-basic.inc"
3 changes: 1 addition & 2 deletions share/target/plus4.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

.define PLUS4

; TODO: Create own encodings file.
.include "c64-encodings.inc"
.include "commodore-264.inc"

2 changes: 1 addition & 1 deletion src/Linker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void Linker::link() {
}
}
if (!object->address) {
FileReader::global.error({}, "no space left in section '%s'", object->section->name.c_str());
FileReader::global.error({}, "no space left for %s in section %s", object->name.as_string().c_str(), object->section->name.c_str());
continue;
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/Memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "Memory.h"

#include <iomanip>
#include <ostream>
#include <sstream>

Expand Down Expand Up @@ -62,11 +63,12 @@ std::optional<uint64_t> Memory::Bank::allocate(const Range& allowed_range, Memor
continue;
}
auto available_range = it->range.intersect(allowed_range);
available_range.start = Int::align(available_range.start, alignment);
available_range.align(alignment);

if (available_range.size < size) {
continue;
}

if (it->range.size == size) {
it->allocation = allocation;
}
Expand All @@ -78,19 +80,21 @@ std::optional<uint64_t> Memory::Bank::allocate(const Range& allowed_range, Memor
auto old_start = it->range.start;
it->range.set_start(available_range.start + size);
it = blocks.insert(it, Block(allocation, {available_range.start, size}));
blocks.insert(it, Block(FREE, {old_start, available_range.start - old_start}));
it = blocks.insert(it, Block(FREE, {old_start, available_range.start - old_start}));
it = std::next(it);
}

if (it != blocks.begin()) {
auto previous = std::prev(it);
if (previous->allocation == it->allocation && previous->range.end() == it->range.start) {
it->range.set_start(previous->range.start);
blocks.erase(previous);
if (previous->allocation == it->allocation && previous->range.end() + 1 == it->range.start) {
previous->range.set_end(it->range.end());
blocks.erase(it);
it = previous;
}
}
if (it != blocks.end()) {
auto next = std::next(it);
if (next->allocation == it->allocation && next->range.start == it->range.end()) {
if (next->allocation == it->allocation && next->range.start == it->range.end() + 1) {
it->range.set_end(next->range.end());
blocks.erase(next);
}
Expand Down Expand Up @@ -137,7 +141,7 @@ std::string Memory::Bank::data(const Range& requested_range) const {
void Memory::Bank::debug_blocks(std::ostream& stream) const {
stream << "allocation blocks:" << std::endl;
for (const auto& block : blocks) {
stream << " " << block.range.start << "-" << block.range.end() << ": ";
stream << std::hex << std::setfill('0') << " " << std::setw(4) << block.range.start << "-" << std::setw(4) << block.range.end() << ": ";
switch (block.allocation) {
case DATA:
stream << "data";
Expand Down
1 change: 0 additions & 1 deletion src/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,4 @@ class Memory {
std::vector<Bank> banks;
};


#endif //ACCELERATE_MEMORY_H
1 change: 0 additions & 1 deletion src/MemoryMap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ bool MemoryMap::Section::operator<(const MemoryMap::Section &other) const {
if (it_other != other.blocks.end()) {
return true;
}
return name < other.name;
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Range.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void Range::set_end(uint64_t new_end) {
if (new_end < start) {
throw Exception("out of memory");
}
size = new_end - start;
size = new_end - start + 1;
}

Range Range::add(const Range &other) const {
Expand Down
2 changes: 2 additions & 0 deletions src/Range.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef ACCELERATE_RANGE_H
#define ACCELERATE_RANGE_H

#include "Int.h"
#include <cstdint>
#include <ostream>

Expand All @@ -52,6 +53,7 @@ class Range {

void add_left(uint64_t amount);
void add_right(uint64_t amount) {size += amount;}
void align(uint alignment) {set_start(Int::align(start, alignment));}
void remove_left(uint64_t amount);
void remove_right(uint64_t amount);
void set_start(uint64_t new_start);
Expand Down

0 comments on commit 86533d9

Please sign in to comment.