-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.c
158 lines (129 loc) · 3.57 KB
/
mem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) 2015, Sam Silberstein. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License").
// Author: [email protected]
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "sim.h"
int arg_get_value(struct ami_machine *m, struct argument arg) {
if (arg.type == REGISTER) {
return m->R[arg.reg];
} else if (arg.type == ADDRESS) {
return m->mem[mem_get_addr(m, arg)].data;
} else {
raise(m, "Non register/address argument supplied");
}
}
int add_get_value(struct ami_machine *m, struct argument add) {
if (add.type == REGISTER) {
return m->R[add.reg];
} else {
return mem_get_addr(m, add);
}
}
int mem_read(struct ami_machine *m, unsigned int addr) {
if (m->mem[addr].data_type == DATA) {
return m->mem[addr].data;
} else {
raise(m, "Inappropriate memory access, attempted to overwrite instruction");
}
}
int mem_get_addr(struct ami_machine *m, struct argument arg) {
if (arg.type == NUMBER) {
return arg.number;
} else if (arg.type == REGISTER) {
return arg.reg;
} else {
int sum = 0, i;
for (i = 0; i < arg.addc; i++) {
if (arg.add[i].type == REG) {
sum += m->R[arg.add[i].value];
} else {
sum += arg.add[i].value;
}
}
return sum;
}
}
void mem_write(struct ami_machine *m, unsigned int addr, int value) {
if (m->mem[addr].data_type == INSTRUCTION) {
raise(m, "Attempted to overwrite instruction");
} else {
m->mem[addr].data_type == DATA;
m->mem[addr].data = value;
}
}
char * read_stack_entry(struct ami_machine *m, int addr) {
char * memValue;
if (m->mem[addr].data_type == INSTRUCTION) {
memValue = (char *) malloc(strlen(m->mem[addr].instruction) + 1);
strcpy(memValue, m->mem[addr].instruction);
} else {
char buffer[80];
sprintf(buffer, "%i: %i", addr, m->mem[addr].data);
memValue = (char*) malloc(strlen(buffer) + 1);
strcpy(memValue, buffer);
}
return memValue;
}
void dump_segments(struct ami_machine *m) {
printf("dumping segments\n");
}
void dump_stack(struct ami_machine *m, int start) {
printf("Stack entries:\n");
int i, end = (start + 25 > STACK_SIZE) ? STACK_SIZE : start + 25;
for (i = start; i < end; i++) {
if (m->mem[i].data_type == INSTRUCTION) {
printf("%s\n", m->mem[i].instruction);
} else {
printf("%i: %i\n", i, m->mem[i].data);
}
}
}
void dump_mem(struct ami_machine *m, unsigned int addr, int count, int size) {
printf("dumping memory\n");
}
struct stack_entry *allocate_segment(struct ami_machine *m, unsigned int addr, unsigned int size, char *type)
{
printf("Allocating segment\n");
return NULL;
}
void free_segments(struct ami_machine *m)
{
int i;
for (i = 0; i < STACK_SIZE; i++) {
m->mem[i].data = 0;
}
}
void allocate_stack(struct ami_machine *m)
{
char *line;
int line_count = 0;
char *file = readfile(m->filename);
int i;
for (i = 0; i < STACK_SIZE; i++) {
m->mem[i].data_type = DATA;
}
line = strtok(file, "\n");
while (line != NULL) {
m->mem[line_count].instruction = (char*) malloc(strlen(line) + 1);
strcpy(m->mem[line_count].instruction, line);
line = strtok(NULL, "\n");
line_count++;
}
for (i = 0; i < line_count; i++) {
printf("Disassembling line %i\n", i);
m->mem[i] = disasm_instr(m, m->mem[i].instruction);
}
m->slots_used = line_count;
}
void push_arguments(struct ami_machine *m)
{
printf("Pushing arguments\n");
}
struct ami_machine *create_ami_machine()
{
struct ami_machine *m = calloc(sizeof(struct ami_machine), 1);
return m;
}