parametric memory size
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 25 Dec 2017 01:34:06 +0100
changeset 34d7eed4d972de
parent 33 ff150572e8c0
child 35 84356a15828b
parametric memory size
c++/rgb-assembler/Memory.h
c++/rgb-assembler/rgb-assembler.cpp
     1.1 --- a/c++/rgb-assembler/Memory.h	Mon Dec 25 01:27:44 2017 +0100
     1.2 +++ b/c++/rgb-assembler/Memory.h	Mon Dec 25 01:34:06 2017 +0100
     1.3 @@ -20,24 +20,25 @@
     1.4  
     1.5  #include "types.h"
     1.6  
     1.7 -const address_t MEMORY_SIZE = 1024;
     1.8 -
     1.9  class Memory {
    1.10  private:
    1.11  
    1.12  	template<typename T> T logMemoryError(const address_t &index) {
    1.13 -		wprintf(L"memory error: index = %d, sizeof(T) = %d, MEMORY_SIZE = %d\n", index, sizeof (T), MEMORY_SIZE);
    1.14 +		wprintf(L"memory error: index = %d, sizeof(T) = %d, MEMORY_SIZE = %d\n", index, sizeof (T), memorySize);
    1.15  		// TODO: return error value or throw exception
    1.16  		return T();
    1.17  	}
    1.18 +
    1.19  	octet_t * memory;
    1.20 +	address_t memorySize;
    1.21  	address_t index;
    1.22  
    1.23  public:
    1.24  
    1.25 -	Memory() {
    1.26 -		memory = (octet_t*) malloc(MEMORY_SIZE);
    1.27 -		index = 0;
    1.28 +	Memory(const address_t memorySize) {
    1.29 +		this->memory = (octet_t*) malloc(memorySize);
    1.30 +		this->memorySize = memorySize;
    1.31 +		this->index = 0;
    1.32  	}
    1.33  
    1.34  	virtual ~Memory() {
    1.35 @@ -51,7 +52,7 @@
    1.36  	 */
    1.37  	template<typename T> T read() {
    1.38  		// TODO: map higher memory to static hardcoded areas or peripherals
    1.39 -		if (index + sizeof (T) <= MEMORY_SIZE) {
    1.40 +		if (index + sizeof (T) <= memorySize) {
    1.41  			T * value = reinterpret_cast<T*> (memory + index);
    1.42  			index += sizeof (T);
    1.43  			return *value;
    1.44 @@ -66,7 +67,7 @@
    1.45  	 */
    1.46  	template<typename T> T read(const address_t &address) {
    1.47  		// TODO: map higher memory to static hardcoded areas or peripherals
    1.48 -		if (address + sizeof (T) <= MEMORY_SIZE) {
    1.49 +		if (address + sizeof (T) <= memorySize) {
    1.50  			return *(reinterpret_cast<T*> (memory + address));
    1.51  		} else {
    1.52  			return logMemoryError<T>(address);
    1.53 @@ -78,7 +79,7 @@
    1.54  	 * @param value value to be written at current position
    1.55  	 */
    1.56  	template<typename T> T write(const T value) {
    1.57 -		if (index + sizeof (T) <= MEMORY_SIZE) {
    1.58 +		if (index + sizeof (T) <= memorySize) {
    1.59  			T * m = reinterpret_cast<T*> (memory + index);
    1.60  			*m = value;
    1.61  			index += sizeof (value);
    1.62 @@ -92,7 +93,7 @@
    1.63  	 * @param value value to be written at given position
    1.64  	 */
    1.65  	template<typename T> T write(const address_t &address, const T value) {
    1.66 -		if (address + sizeof (T) <= MEMORY_SIZE) {
    1.67 +		if (address + sizeof (T) <= memorySize) {
    1.68  			T * m = reinterpret_cast<T*> (memory + address);
    1.69  			*m = value;
    1.70  		} else {
    1.71 @@ -119,7 +120,7 @@
    1.72  	 * @return whether the current position is inside the memory boundaries = still processing.
    1.73  	 */
    1.74  	bool isNotOver() {
    1.75 -		return index < MEMORY_SIZE;
    1.76 +		return index < memorySize;
    1.77  	}
    1.78  
    1.79  	/**
    1.80 @@ -133,6 +134,6 @@
    1.81  	 * Set current position behind the end of the memory = stop processing.
    1.82  	 */
    1.83  	void setAddressToEnd() {
    1.84 -		index = MEMORY_SIZE;
    1.85 +		index = memorySize;
    1.86  	}
    1.87  };
     2.1 --- a/c++/rgb-assembler/rgb-assembler.cpp	Mon Dec 25 01:27:44 2017 +0100
     2.2 +++ b/c++/rgb-assembler/rgb-assembler.cpp	Mon Dec 25 01:34:06 2017 +0100
     2.3 @@ -36,12 +36,14 @@
     2.4  
     2.5  using namespace std;
     2.6  
     2.7 +const address_t MEMORY_SIZE = 1024;
     2.8 +
     2.9  int main(int argc, char* argv[]) {
    2.10  
    2.11  	setlocale(LC_ALL, "");
    2.12  
    2.13 -	Memory memory;
    2.14 -	
    2.15 +	Memory memory(MEMORY_SIZE);
    2.16 +
    2.17  	// Sample program / data:
    2.18  	// TODO: load bytes from file, stdin, serial port, network…
    2.19  	{