Skip to content

Commit

Permalink
Implement ZX Spectrum TAP target.
Browse files Browse the repository at this point in the history
Loading screen untested.
  • Loading branch information
dillof committed Jun 30, 2024
1 parent 1a335c6 commit be50483
Show file tree
Hide file tree
Showing 34 changed files with 756 additions and 34 deletions.
13 changes: 5 additions & 8 deletions examples/hello-world-zx-spectrum.s
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
.section code

.public start {
ld bc, string
: ld a, (bc)
call CLS
ld hl, string
: ld a,(hl)
cp a, 0
jr z, end
ret z
rst $10
inc bc
inc hl
jr :-

end:
ret
}


.section data

string {
Expand Down
6 changes: 6 additions & 0 deletions share/lib/zx-spectrum.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.target "z80"

.public CLS = $0daf



4 changes: 2 additions & 2 deletions share/target/zx-spectrum-tap-basic.target
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ basic_start = $5ccb
basic .address basic_start .used {
.data LINE_NUMBER:big_endian(2), line_end - line_start:2
line_start:
.data "{{clear}}{val}}\"", program_start:string, "\":"
.data "{{poke}}{{val}}\"23739\",{{val}}\"111:\""
.data "{{clear}}{{val}}\"", program_start - 1:string, "\":"
.data "{{poke}}{{val}}\"23739\",{{val}}\"111\":"
.if INCLUDE_LOADING_SCREEN {
.data "{{load}}\"\"{{screen$}}:"
}
Expand Down
10 changes: 4 additions & 6 deletions share/target/zx-spectrum-tap.inc
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ header_start:
.data name:string(10) ; file name
.data end - start + 1:2, parameter_1:2, parameter_2:2 ; length, parameters
header_end:
; .checksum "xor", header_start, header_end - 1
.data $00 ; TODO
.checksum xor, header_start, header_end - 1

; data block
.data end - start + 3:2; size
.data end - start + 3:2 ; size
data_start:
.data $ff ; block type
.memory start, end
.data_end:
; .checksum xor, data_start, data_end - 1
.data $00 ; TODO: checksum
data_end:
.checksum xor, data_start, data_end - 1
}
23 changes: 21 additions & 2 deletions src/BodyParser.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 "BodyParser.h"

#include "ChecksumBody.h"
#include "ExpressionNode.h"
#include "ExpressionParser.h"
#include "FileReader.h"
Expand All @@ -42,6 +43,7 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

const Symbol BodyParser::symbol_pc = Symbol(".pc");
const Token BodyParser::token_binary_file = Token(Token::DIRECTIVE, "binary_file");
const Token BodyParser::token_checksum = Token(Token::DIRECTIVE, "checksum");
const Token BodyParser::token_data = Token(Token::DIRECTIVE, "data");
const Token BodyParser::token_end = Token(Token::DIRECTIVE, "end");
const Token BodyParser::token_else = Token(Token::DIRECTIVE, "else");
Expand All @@ -54,7 +56,20 @@ const Token BodyParser::token_repeat = Token(Token::DIRECTIVE, "repeat");
const Token BodyParser::token_scope = Token(Token::DIRECTIVE, "scope");
const Token BodyParser::token_start = Token(Token::DIRECTIVE, "start");

const std::unordered_map<Symbol, void (BodyParser::*)()> BodyParser::directive_parser_methods = {{token_binary_file.as_symbol(), &BodyParser::parse_binary_file}, {token_data.as_symbol(), &BodyParser::parse_data}, {token_else.as_symbol(), &BodyParser::parse_else}, {token_else_if.as_symbol(), &BodyParser::parse_else_if}, {token_error.as_symbol(), &BodyParser::parse_error}, {token_if.as_symbol(), &BodyParser::parse_if}, {token_memory.as_symbol(), &BodyParser::parse_memory}, {token_repeat.as_symbol(), &BodyParser::parse_repeat}, {token_scope.as_symbol(), &BodyParser::parse_scope}};
// clang-format off
const std::unordered_map<Symbol, void (BodyParser::*)()> BodyParser::directive_parser_methods = {
{token_binary_file.as_symbol(), &BodyParser::parse_binary_file},
{token_checksum.as_symbol(), &BodyParser::parse_checksum},
{token_data.as_symbol(), &BodyParser::parse_data},
{token_else.as_symbol(), &BodyParser::parse_else},
{token_else_if.as_symbol(), &BodyParser::parse_else_if},
{token_error.as_symbol(), &BodyParser::parse_error},
{token_if.as_symbol(), &BodyParser::parse_if},
{token_memory.as_symbol(), &BodyParser::parse_memory},
{token_repeat.as_symbol(), &BodyParser::parse_repeat},
{token_scope.as_symbol(), &BodyParser::parse_scope}
};
// clang-format on

void BodyParser::setup(FileTokenizer& tokenizer) {
tokenizer.add_literal(Token::colon_minus);
Expand Down Expand Up @@ -189,12 +204,16 @@ Expression BodyParser::get_pc(Symbol label) const {

void BodyParser::parse_directive(const Token& directive) {
auto it = directive_parser_methods.find(directive.as_symbol());
if (it == directive_parser_methods.end() || (directive == token_memory && !allow_memory())) {
if (it == directive_parser_methods.end() || ((directive == token_memory || directive == token_checksum) && !allow_memory())) {
throw ParseException(directive, "unknown directive");
}
(this->*it->second)();
}

void BodyParser::parse_checksum() {
current_body->append(ChecksumBody::parse(tokenizer));
}

void BodyParser::parse_data() {
EvaluationResult result;
auto data = ExpressionParser(tokenizer).parse_list();
Expand Down
2 changes: 2 additions & 0 deletions src/BodyParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class BodyParser {

void handle_name(Visibility visibility, const Token& name);
void parse_binary_file();
void parse_checksum();
void parse_data();
void parse_error();
void parse_else();
Expand All @@ -166,6 +167,7 @@ class BodyParser {

static const Symbol symbol_pc;
static const Token token_binary_file;
static const Token token_checksum;
static const Token token_data;
static const Token token_end;
static const Token token_else;
Expand Down
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ add_library(xlr8-library STATIC
CPUGetter.cc
CPUParser.cc
Callable.cc
Checksum.cc
ChecksumBody.cc
ChecksumAlgorithm.cc
ChecksumAlgorithmXor.cc
ChecksumComputation.cc
Command.cc
Commandline.cc
DataBody.cc
Expand All @@ -29,6 +34,7 @@ add_library(xlr8-library STATIC
EvaluationContext.cc
EvaluationResult.cc
Exception.cc
ExistsExpression.cc
Expression.cc
ExpressionNode.cc
ExpressionParser.cc
Expand Down
32 changes: 32 additions & 0 deletions src/Checksum.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Checksum.cc --
Copyright (C) Dieter Baron
The authors can be contacted at <[email protected]>
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 "Checksum.h"
51 changes: 51 additions & 0 deletions src/Checksum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef CHECKSUM_H
#define CHECKSUM_H

/*
Checksum.h --
Copyright (C) Dieter Baron
The authors can be contacted at <[email protected]>
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 <cstdint>

#include "Symbol.h"
#include "Value.h"

class Checksum {
public:
class Invocation {
Symbol algorithm{};
std::vector<Value> parameters;
};


};


#endif // CHECKSUM_H
52 changes: 52 additions & 0 deletions src/ChecksumAlgorithm.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
ChecksumAlgorithm.cc --
Copyright (C) Dieter Baron
The authors can be contacted at <[email protected]>
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 "ChecksumAlgorithm.h"

#include "ChecksumAlgorithmXor.h"

// clang-format off
const std::unordered_map<Symbol, std::shared_ptr<ChecksumAlgorithm>(*)(Symbol)> ChecksumAlgorithm::algorithms = {
{Symbol{"xor"}, &ChecksumAlgorithmXor::create}
};
// clang-format on

const std::unordered_set<Symbol> ChecksumAlgorithm::no_parameters{};

std::shared_ptr<ChecksumAlgorithm> ChecksumAlgorithm::create(Symbol algorithm_name) {
auto it = algorithms.find(algorithm_name);

if (it != algorithms.end()) {
return it->second(algorithm_name);
}

throw Exception("unknown checksum algorithm " + algorithm_name.str());
}
61 changes: 61 additions & 0 deletions src/ChecksumAlgorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef CHECKSUM_ALGORITHM_H
#define CHECKSUM_ALGORITHM_H

/*
ChecksumAlgorithm.h --
Copyright (C) Dieter Baron
The authors can be contacted at <[email protected]>
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 <cstdint>

#include "Symbol.h"
#include "Tokenizer.h"
#include "Value.h"

class ChecksumAlgorithm {
public:
ChecksumAlgorithm(Symbol name): name{name} {}
virtual ~ChecksumAlgorithm() = default;

static std::shared_ptr<ChecksumAlgorithm> create(Symbol algorithm_name);

virtual std::string compute(std::string::const_iterator begin, std::string::const_iterator end, const std::unordered_map<Symbol, Value>& parameters) = 0;
virtual uint64_t result_size() = 0;
virtual const std::unordered_set<Symbol>& parameter_names() const {return no_parameters;}

Symbol name;

private:
static const std::unordered_map<Symbol, std::shared_ptr<ChecksumAlgorithm>(*)(Symbol)> algorithms;

static const std::unordered_set<Symbol> no_parameters;
};


#endif // CHECKSUM_ALGORITHM_H
Loading

0 comments on commit be50483

Please sign in to comment.