/* Author : Rick van der Zwet * S-number : 0433373 * Version : $Id: memory.c 365 2007-12-03 08:54:04Z rick $ * Copyright : FreeBSD Licence * Description : Memory unit */ #include #include #include #include "memory.h" /* Initial value */ boolean mem_busy = FALSE; void readmem() { int address = 0; uint32_t input = 0; //read 4-byte integer numbers in hexadecimal from input while (scanf("%x", &input) != EOF) { mem_operation(address * 4, input, WRITE_WORD); address++; } //zero the end for (; address < 1024; address++) mem_operation(address * 4, 0, WRITE_WORD); } void writemem() { int zeros = 0; int address = 0; word value = 0; for (address = 0; address < 1024; address++) { value = mem_operation(address * 4, 0, READ); if (value == 0) zeros++; else { for (; zeros > 0; zeros--) printf("%#010x\n", 0); printf("%#010x\n", value); } } } word mem_operation(const word address, const word value, const mem_control_t action) { static union memword_t memory[1024]; int coreAddress = address / 4; int8_t offset = address % 4; if (coreAddress > 1024) { fprintf(stderr, "Memory which is greater than 1024 word, '%i'\n", coreAddress); exit(EX_SOFTWARE); } switch (action) { case READ: if (offset != 0) { fprintf(stderr, "Read needs mod 4 bytes alligned\n"); exit(EX_SOFTWARE); } else { mem_busy = FALSE; return(memory[coreAddress].fullword); } break; case WRITE_BYTE: memory[coreAddress].byte[offset] = (byte)value; break; case WRITE_HALFWORD: if ((offset % 2) != 0) { fprintf(stderr, "Halfword need mod 16 bits alligned\n"); exit(EX_SOFTWARE); } memory[coreAddress].halfword[offset / 2] = (halfword)value; break; case WRITE_WORD: if (offset != 0) { fprintf(stderr, "Write needs mod 4 bytes alligned\n"); exit(EX_SOFTWARE); } memory[coreAddress].fullword = value; break; default: fprintf(stderr, "Memory control '%i' not implemented\n", action); exit(EX_SOFTWARE); break; }; mem_busy = FALSE; return(0); }