Skip to content

Commit

Permalink
Add ZX Spectrum NEX target.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Jul 1, 2024
1 parent be50483 commit 3637ac8
Show file tree
Hide file tree
Showing 16 changed files with 273 additions and 21 deletions.
49 changes: 49 additions & 0 deletions share/target/zx-spectrum-nex.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.default .public NEX_BORDER_COLOR = 7
.default .public NEX_LOAD_BANK_DELAY = 0
.default .public NEX_LOAD_START_DELAY = 0

bank_start(bank) = bank * $4000
bank_end(bank) = bank * $4000 + $3fff
bank(address) = (address) >> 14

.macro bank bank, data_start, data_end {
.if bank(data_start) <= bank && bank(data_end) >= bank {
.memory bank_start(bank), bank_end(bank)
}
}

.output {
; Header
.data "NextV1.2" ; signature and version
.data 0 ; 768k RAM is enough
.data bank(.data_end) - bank(.data_start) + 1 ; number of 16k banks
.data 0 ; TODO: loading screens
.data NEX_BORDER_COLOR
.data $ff58 ; stack pointer
.data start:2 ; entry point
.data 0:2 ; number of extra files
.repeat j, 112 { ; included 16k banks
.if bank(.data_start) <= j && bank(.data_end) >= j {
.data 1
}
.else {
.data 0
}
}
.data 0, 0 ; loading bar enable and color
.data NEX_LOAD_BANK_DELAY, NEX_LOAD_START_DELAY ; loading delays in frames
.data 0 ; reset machine state
.data 0, 0, 0 ; TODO: required core version
.data 0 ; Timex Hires mode color
.data 0 ; entry bank
.data 0:2 ; close file
.data .fill(370, 0) ; reserved
; TODO: loading screen
bank 5, .data_start, .data_end
bank 2, .data_start, .data_end
bank 0, .data_start, .data_end
bank 1, .data_start, .data_end
bank 3, .data_start, .data_end
bank 4, .data_start, .data_end
; TODO: banks 6 - 111
}
25 changes: 25 additions & 0 deletions share/target/zx-spectrum-nex.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.define ZX_SPECTRUM_NEXT
.cpu "z80"

.include "zx-spectrum-encoding.inc"
.include "zx-spectrum-nex.inc"

.default .public program_start = $8000
.default .public program_end = $ffff

.segment program {
address: program_start - program_end
}

.section code {
segment: program
}

.section data {
segment: program
}

.section reserved {
type: reserve_only
segment: program
}
2 changes: 2 additions & 0 deletions share/target/zx-spectrum-tap.inc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.extension "tap"

.macro tap_file_program name, start, end, line_number {
tap_file TAP_FILE_PROGRAM, name, start, end, line_number, end - start + 1
}
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ add_library(xlr8-library STATIC
Memory.cc
MemoryBody.cc
MemoryMap.cc
MinMaxExpression.cc
Node.cc
Object.cc
ObjectExpression.cc
Expand Down
2 changes: 0 additions & 2 deletions src/FillExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,4 @@ class FillExpression: public BaseExpression {
Expression value;
};



#endif //FILLEXPRESSION_H
5 changes: 5 additions & 0 deletions src/FunctionExpression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Function.h"
#include "InRangeExpression.h"
#include "LabelExpression.h"
#include "MinMaxExpression.h"
#include "ObjectFileParser.h"
#include "SizeofExpression.h"

// clang-format off
const std::unordered_map<Symbol, Expression (*)(const std::vector<Expression>&)> FunctionExpression::builtin_functions = {
{Symbol(".defined"), &DefinedExpression::create},
{Symbol{".exists"}, &ExistsExpression::create},
{Symbol{".fill"}, &FillExpression::create},
{Symbol{".max"}, &MinMaxExpression::create_max},
{Symbol{".min"}, &MinMaxExpression::create_min},
{Symbol(".sizeof"), &SizeofExpression::create}
};
// clang-format on

void FunctionExpression::serialize_sub(std::ostream &stream) const {
stream << name << "(";
Expand Down
12 changes: 8 additions & 4 deletions src/IntegerEncoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Encoder.h"
#include "Exception.h"
#include "Int.h"
#include "Target.h"

uint64_t IntegerEncoder::default_byte_order;
const uint64_t IntegerEncoder::big_endian_byte_order = 87654321;
const uint64_t IntegerEncoder::little_endian_byte_order = 12345678;

uint64_t IntegerEncoder::default_byte_order() {
return Target::current_target && Target::current_target->cpu ? Target::current_target->cpu->byte_order : little_endian_byte_order;
}

std::ostream& operator<<(std::ostream& stream, const IntegerEncoder& encoding) {
encoding.serialize(stream);
return stream;
Expand Down Expand Up @@ -83,16 +87,16 @@ bool IntegerEncoder::fits(const Value& value) const { return value.is_integer()
bool IntegerEncoder::is_natural_encoder(const Value& value) const {
switch (type) {
case SIGNED:
return value.is_signed() && value.default_size() == size && byte_order() == default_byte_order;
return value.is_signed() && value.default_size() == size && byte_order() == default_byte_order();

case UNSIGNED:
return value.is_unsigned() && value.default_size() == size && byte_order() == default_byte_order;
return value.is_unsigned() && value.default_size() == size && byte_order() == default_byte_order();
}
throw Exception("internal error: invalid integer encoder type %d", type);
}

void IntegerEncoder::serialize(std::ostream& stream) const {
if (byte_order() == default_byte_order && size) {
if (byte_order() == default_byte_order() && size) {
switch (type) {
case SIGNED:
stream << ":-" << *size;
Expand Down
4 changes: 2 additions & 2 deletions src/IntegerEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class IntegerEncoder: public BaseEncoder {
bool operator==(const IntegerEncoder& other) const;
bool operator!=(const IntegerEncoder& other) const {return !(*this == other);}

static uint64_t default_byte_order;
static uint64_t default_byte_order();
static const uint64_t little_endian_byte_order;
static const uint64_t big_endian_byte_order;

Expand All @@ -70,7 +70,7 @@ class IntegerEncoder: public BaseEncoder {
std::optional<size_t> size;
std::optional<uint64_t> explicit_byte_order;

[[nodiscard]] uint64_t byte_order() const {return explicit_byte_order.value_or(default_byte_order);}
[[nodiscard]] uint64_t byte_order() const {return explicit_byte_order.value_or(default_byte_order());}
};

std::ostream& operator<<(std::ostream& stream, const IntegerEncoder& encoding);
Expand Down
1 change: 0 additions & 1 deletion src/Linker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ void Linker::set_target(const Target* new_target) {
target = new_target;
program->private_environment->add_next(target->object_file->public_environment);
memory = target->map.initialize_memory();
IntegerEncoder::default_byte_order = target->cpu->byte_order;
}

void Linker::add_library(std::shared_ptr<ObjectFile> library) {
Expand Down
20 changes: 9 additions & 11 deletions src/Memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ Memory::Bank::Bank(Range range) : range(range) {
blocks.emplace_back(FREE, range);
}

void Memory::Bank::copy(uint64_t address, const std::string& data) { memory.replace(address, data.size(), data); }
void Memory::Bank::copy(uint64_t address, const std::string& data) {
memory.replace(offset(address), data.size(), data);
}

std::optional<uint64_t> Memory::Bank::allocate(const Range& allowed_range, Memory::Allocation allocation, uint64_t alignment, uint64_t size) {
if (allowed_range.size < size) {
Expand Down Expand Up @@ -116,17 +118,13 @@ Range Memory::Bank::data_range() const {
}
}

std::string Memory::Bank::data(const Range& range) const {
if (range.start >= memory.size()) {
return std::string(range.size, 0);
}
if (range.start + range.size > memory.size()) {
auto padding = range.start + range.size - memory.size();
return memory.substr(range.start) + std::string(padding, 0);
}
else {
return memory.substr(range.start, range.size);
std::string Memory::Bank::data(const Range& requested_range) const {
auto result = std::string(requested_range.size, 0);
auto overlap_range = requested_range.intersect(range);
if (!overlap_range.empty()) {
result.replace(overlap_range.start - requested_range.start, overlap_range.size, memory, offset(overlap_range.start), overlap_range.size);
}
return result;
}

void Memory::Bank::debug_blocks(std::ostream& stream) const {
Expand Down
4 changes: 3 additions & 1 deletion src/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Memory {

[[nodiscard]] Range data_range() const;

[[nodiscard]] std::string data(const Range& range) const;
[[nodiscard]] std::string data(const Range& requested_range) const;

void debug_blocks(std::ostream& stream) const;

Expand All @@ -69,6 +69,8 @@ class Memory {
Range range;
};

size_t offset(size_t address) const {return address - range.start;}

std::string memory;
Range range;
std::list<Block> blocks;
Expand Down
96 changes: 96 additions & 0 deletions src/MinMaxExpression.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
MinMaxExpression.cc --
Copyright (C) Dieter Baron
The authors can be contacted at <accelerate@tpau.group>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "MinMaxExpression.h"

#include "ParseException.h"

Expression MinMaxExpression::create(bool minimum, const std::vector<Expression>& arguments) {
if (arguments.size() != 2) {
throw ParseException(Location(), "invalid number of arguments");
}
return create(arguments[0], arguments[1], minimum);
}

Expression MinMaxExpression::create(const Expression& a, const Expression& b, bool minimum) {
// TODO: validate and propagate types.
if (a.has_value() && b.has_value()) {
return Expression(min_max(minimum, *a.value(), *b.value()));
}
else {
return Expression(std::make_shared<MinMaxExpression>(a, b, minimum));
}
}

std::optional<Expression> MinMaxExpression::evaluated(const EvaluationContext& context) const {
auto new_a = a.evaluated(context);
auto new_b = b.evaluated(context);
if (new_a || new_b) {
return create(new_a.value_or(a), new_b.value_or(b), minimum);
}
else {
return {};
}
}

void MinMaxExpression::serialize_sub(std::ostream& stream) const {
stream << (minimum ? ".min" : ".max") << "(" << a << ", " << b << ")";
}

std::optional<Value> MinMaxExpression::minimum_value() const {
const auto a_min = a.minimum_value();
const auto b_min = b.minimum_value();

if (a_min && b_min) {
return min_max(minimum, *a_min, *b_min);
}
else {
return {};
}
}

std::optional<Value> MinMaxExpression::maximum_value() const {
const auto a_max = a.maximum_value();
const auto b_max = b.maximum_value();

if (a_max && b_max) {
return min_max(minimum, *a_max, *b_max);
}
else {
return {};
}
}

Value MinMaxExpression::min_max(bool minimum, const Value& a, const Value& b) {
auto a_smaller = a < b;

return (minimum && a_smaller) || (!minimum && !a_smaller) ? a : b;
}
64 changes: 64 additions & 0 deletions src/MinMaxExpression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef MIN_MAX_EXPRESSION_H
#define MIN_MAX_EXPRESSION_H

/*
MinMaxExpression.h --
Copyright (C) Dieter Baron
The authors can be contacted at <accelerate@tpau.group>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "BaseExpression.h"
#include "Expression.h"

class MinMaxExpression: public BaseExpression {
public:
explicit MinMaxExpression(Expression a, Expression b, bool minimum): a{std::move(a)}, b{std::move(b)}, minimum{minimum} {}

static Expression create_min(const std::vector<Expression>& arguments) {return create(true, arguments);}
static Expression create_max(const std::vector<Expression>& arguments) {return create(false, arguments);}
static Expression create(const Expression& a, const Expression& b, bool minimum);

[[nodiscard]] std::optional<Expression> evaluated(const EvaluationContext& context) const override;

[[nodiscard]] std::optional<Value> minimum_value() const override;
[[nodiscard]] std::optional<Value> maximum_value() const override;

protected:
void serialize_sub(std::ostream& stream) const override;

private:
static Expression create(bool minimum, const std::vector<Expression>& arguments);
static Value min_max(bool minimum, const Value& a, const Value& b);

Expression a;
Expression b;
bool minimum;
};


#endif // MIN_MAX_EXPRESSION_H
Loading

0 comments on commit 3637ac8

Please sign in to comment.