move memory functions to a header file + introduce Command classes
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 23 Dec 2017 23:24:51 +0100
changeset 20b9ceffdcaf14
parent 19 17785b69430d
child 21 ae4a9092e1a8
move memory functions to a header file + introduce Command classes
c++/rgb-assembler/Command.h
c++/rgb-assembler/commands/Goto.h
c++/rgb-assembler/memory.h
c++/rgb-assembler/nbproject/configurations.xml
c++/rgb-assembler/rgb-assembler.cpp
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/c++/rgb-assembler/Command.h	Sat Dec 23 23:24:51 2017 +0100
     1.3 @@ -0,0 +1,16 @@
     1.4 +#pragma once
     1.5 +
     1.6 +#include "types.h"
     1.7 +#include "memory.h"
     1.8 +
     1.9 +class Command {
    1.10 +public:
    1.11 +	/**
    1.12 +	 * Process command at given address, read parameters if any. And shift address to new one (after last parameter)
    1.13 +	 * @param memory
    1.14 +	 * @param index address of the command in the memory
    1.15 +	 */
    1.16 +	virtual void process(octet_t * memory, address_t &index) = 0;
    1.17 +private:
    1.18 +
    1.19 +};
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/c++/rgb-assembler/commands/Goto.h	Sat Dec 23 23:24:51 2017 +0100
     2.3 @@ -0,0 +1,20 @@
     2.4 +#pragma once
     2.5 +
     2.6 +#include <wchar.h>
     2.7 +
     2.8 +#include "../Command.h"
     2.9 +
    2.10 +namespace commands {
    2.11 +
    2.12 +class Goto : public Command {
    2.13 +public:
    2.14 +
    2.15 +	void process(octet_t* memory, address_t& index) override {
    2.16 +		index = read<address_t>(memory, index);
    2.17 +		wprintf(L"GOTO %*d\n", 5, index);
    2.18 +	}
    2.19 +private:
    2.20 +
    2.21 +};
    2.22 +
    2.23 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/c++/rgb-assembler/memory.h	Sat Dec 23 23:24:51 2017 +0100
     3.3 @@ -0,0 +1,45 @@
     3.4 +#pragma once
     3.5 +
     3.6 +#include "types.h"
     3.7 +
     3.8 +const address_t MEMORY_SIZE = 1024;
     3.9 +
    3.10 +template<typename T> T logMemoryError(const address_t &index) {
    3.11 +	wprintf(L"memory error: index = %d, sizeof(T) = %d, MEMORY_SIZE = %d\n", index, sizeof (T), MEMORY_SIZE);
    3.12 +	// TODO: return error value or throw exception
    3.13 +	return T();
    3.14 +}
    3.15 +
    3.16 +/**
    3.17 + * Reads data on given position in memory and increments the index (position).
    3.18 + * 
    3.19 + * @param memory array of bytes / octets
    3.20 + * @param index offset in same units as memory type
    3.21 + * @return value found at given position
    3.22 + */
    3.23 +template<typename T> T read(octet_t * memory, address_t &index) {
    3.24 +	// TODO: map higher memory to static hardcoded areas or peripherals
    3.25 +	if (index + sizeof (T) <= MEMORY_SIZE) {
    3.26 +		T * value = reinterpret_cast<T*> (memory + index);
    3.27 +		index += sizeof (T);
    3.28 +		return *value;
    3.29 +	} else {
    3.30 +		return logMemoryError<T>(index);
    3.31 +	}
    3.32 +}
    3.33 +
    3.34 +/**
    3.35 + * Writes data to given position in memory and increments the index (position).
    3.36 + * @param memory array of bytes / octets
    3.37 + * @param index offset in same units as memory type
    3.38 + * @param value value to be written at given position
    3.39 + */
    3.40 +template<typename T> T write(octet_t * memory, address_t &index, const T value) {
    3.41 +	if (index + sizeof (T) <= MEMORY_SIZE) {
    3.42 +		T * m = reinterpret_cast<T*> (memory + index);
    3.43 +		*m = value;
    3.44 +		index += sizeof (value);
    3.45 +	} else {
    3.46 +		return logMemoryError<T>(index);
    3.47 +	}
    3.48 +}
     4.1 --- a/c++/rgb-assembler/nbproject/configurations.xml	Sat Dec 23 20:13:24 2017 +0100
     4.2 +++ b/c++/rgb-assembler/nbproject/configurations.xml	Sat Dec 23 23:24:51 2017 +0100
     4.3 @@ -4,6 +4,9 @@
     4.4      <logicalFolder name="HeaderFiles"
     4.5                     displayName="Header Files"
     4.6                     projectFiles="true">
     4.7 +      <itemPath>Command.h</itemPath>
     4.8 +      <itemPath>commands/Goto.h</itemPath>
     4.9 +      <itemPath>memory.h</itemPath>
    4.10        <itemPath>types.h</itemPath>
    4.11      </logicalFolder>
    4.12      <logicalFolder name="ResourceFiles"
    4.13 @@ -41,6 +44,12 @@
    4.14            <standard>8</standard>
    4.15          </ccTool>
    4.16        </compileType>
    4.17 +      <item path="Command.h" ex="false" tool="3" flavor2="0">
    4.18 +      </item>
    4.19 +      <item path="commands/Goto.h" ex="false" tool="3" flavor2="0">
    4.20 +      </item>
    4.21 +      <item path="memory.h" ex="false" tool="3" flavor2="0">
    4.22 +      </item>
    4.23        <item path="rgb-assembler.cpp" ex="false" tool="1" flavor2="0">
    4.24        </item>
    4.25        <item path="types.h" ex="false" tool="3" flavor2="0">
    4.26 @@ -67,6 +76,12 @@
    4.27            <developmentMode>5</developmentMode>
    4.28          </asmTool>
    4.29        </compileType>
    4.30 +      <item path="Command.h" ex="false" tool="3" flavor2="0">
    4.31 +      </item>
    4.32 +      <item path="commands/Goto.h" ex="false" tool="3" flavor2="0">
    4.33 +      </item>
    4.34 +      <item path="memory.h" ex="false" tool="3" flavor2="0">
    4.35 +      </item>
    4.36        <item path="rgb-assembler.cpp" ex="false" tool="1" flavor2="0">
    4.37        </item>
    4.38        <item path="types.h" ex="false" tool="3" flavor2="0">
     5.1 --- a/c++/rgb-assembler/rgb-assembler.cpp	Sat Dec 23 20:13:24 2017 +0100
     5.2 +++ b/c++/rgb-assembler/rgb-assembler.cpp	Sat Dec 23 23:24:51 2017 +0100
     5.3 @@ -3,18 +3,19 @@
     5.4  #include <wchar.h>
     5.5  #include <locale.h>
     5.6  #include <cstring>
     5.7 -
     5.8 +#include <unordered_map>
     5.9  #include <chrono>
    5.10  #include <thread>
    5.11  
    5.12  #include "types.h"
    5.13 +#include "memory.h"
    5.14 +#include "Command.h"
    5.15 +#include "commands/Goto.h"
    5.16  
    5.17  using namespace std;
    5.18  
    5.19  // TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ?
    5.20  
    5.21 -const address_t MEMORY_SIZE = 1024;
    5.22 -
    5.23  /**
    5.24   * Skip to the given address.
    5.25   * parameter: address_t
    5.26 @@ -71,46 +72,6 @@
    5.27  
    5.28  // TODO: more commands, better numbers
    5.29  
    5.30 -template<typename T> T logMemoryError(const address_t &index) {
    5.31 -	wprintf(L"memory error: index = %d, sizeof(T) = %d, MEMORY_SIZE = %d\n", index, sizeof (T), MEMORY_SIZE);
    5.32 -	// TODO: return error value or throw exception
    5.33 -	return T();
    5.34 -}
    5.35 -
    5.36 -/**
    5.37 - * Reads data on given position in memory and increments the index (position).
    5.38 - * 
    5.39 - * @param memory array of bytes / octets
    5.40 - * @param index offset in same units as memory type
    5.41 - * @return value found at given position
    5.42 - */
    5.43 -template<typename T> T read(octet_t * memory, address_t &index) {
    5.44 -	// TODO: map higher memory to static hardcoded areas or peripherals
    5.45 -	if (index + sizeof (T) <= MEMORY_SIZE) {
    5.46 -		T * value = reinterpret_cast<T*> (memory + index);
    5.47 -		index += sizeof (T);
    5.48 -		return *value;
    5.49 -	} else {
    5.50 -		return logMemoryError<T>(index);
    5.51 -	}
    5.52 -}
    5.53 -
    5.54 -/**
    5.55 - * Writes data to given position in memory and increments the index (position).
    5.56 - * @param memory array of bytes / octets
    5.57 - * @param index offset in same units as memory type
    5.58 - * @param value value to be written at given position
    5.59 - */
    5.60 -template<typename T> T write(octet_t * memory, address_t &index, const T value) {
    5.61 -	if (index + sizeof (T) <= MEMORY_SIZE) {
    5.62 -		T * m = reinterpret_cast<T*> (memory + index);
    5.63 -		*m = value;
    5.64 -		index += sizeof (value);
    5.65 -	} else {
    5.66 -		return logMemoryError<T>(index);
    5.67 -	}
    5.68 -}
    5.69 -
    5.70  int main(int argc, char* argv[]) {
    5.71  
    5.72  	setlocale(LC_ALL, "");