source: liacs/ca/opdr2b/src/memory.c@ 6

Last change on this file since 6 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

  • Property svn:executable set to *
File size: 2.4 KB
Line 
1/* Author : Rick van der Zwet
2 * S-number : 0433373
3 * Version : $Id: memory.c 365 2007-12-03 08:54:04Z rick $
4 * Copyright : FreeBSD Licence
5 * Description : Memory unit
6 */
7
8#include <stdlib.h>
9#include <stdio.h>
10#include <sysexits.h>
11#include "memory.h"
12
13/* Initial value */
14boolean mem_busy = FALSE;
15
16
17void
18readmem()
19{
20 int address = 0;
21 uint32_t input = 0;
22
23 //read 4-byte integer numbers in hexadecimal from input
24 while (scanf("%x", &input) != EOF) {
25 mem_operation(address * 4, input, WRITE_WORD);
26 address++;
27 }
28 //zero the end
29 for (; address < 1024; address++)
30 mem_operation(address * 4, 0, WRITE_WORD);
31}
32
33
34void
35writemem()
36{
37 int zeros = 0;
38 int address = 0;
39 word value = 0;
40 for (address = 0; address < 1024; address++) {
41 value = mem_operation(address * 4, 0, READ);
42 if (value == 0)
43 zeros++;
44 else {
45 for (; zeros > 0; zeros--)
46 printf("%#010x\n", 0);
47 printf("%#010x\n", value);
48 }
49 }
50}
51
52
53word
54mem_operation(const word address, const word value,
55 const mem_control_t action)
56{
57 static union memword_t memory[1024];
58
59 int coreAddress = address / 4;
60 int8_t offset = address % 4;
61
62 if (coreAddress > 1024) {
63 fprintf(stderr, "Memory which is greater than 1024 word, '%i'\n",
64 coreAddress);
65 exit(EX_SOFTWARE);
66 }
67
68 switch (action) {
69 case READ:
70 if (offset != 0) {
71 fprintf(stderr, "Read needs mod 4 bytes alligned\n");
72 exit(EX_SOFTWARE);
73 } else {
74 mem_busy = FALSE;
75 return(memory[coreAddress].fullword);
76 }
77 break;
78 case WRITE_BYTE:
79 memory[coreAddress].byte[offset] = (byte)value;
80 break;
81 case WRITE_HALFWORD:
82 if ((offset % 2) != 0) {
83 fprintf(stderr, "Halfword need mod 16 bits alligned\n");
84 exit(EX_SOFTWARE);
85 }
86 memory[coreAddress].halfword[offset / 2] = (halfword)value;
87 break;
88 case WRITE_WORD:
89 if (offset != 0) {
90 fprintf(stderr, "Write needs mod 4 bytes alligned\n");
91 exit(EX_SOFTWARE);
92 }
93 memory[coreAddress].fullword = value;
94 break;
95 default:
96 fprintf(stderr, "Memory control '%i' not implemented\n", action);
97 exit(EX_SOFTWARE);
98 break;
99 };
100 mem_busy = FALSE;
101 return(0);
102}
Note: See TracBrowser for help on using the repository browser.