-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram_counter.c
223 lines (179 loc) · 5.45 KB
/
program_counter.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <stdio.h>
#include <stdlib.h>
#include "program_counter.h"
#include "bitmath.h"
#include "assembler.h"
#include <string.h>
void increment_counter(struct program_counter *pc)
{
if(pc->address_mode == 1 && pc->offset == 0)
{
pc->offset = 1;
return;
}
if(pc->current_address < 4096)
{
pc->current_address+=1;
if(pc->address_mode == 1)
pc->offset = 0;
}
else
fprintf(stderr,"Program counter reached end of memory");
}
void decrement_counter(struct program_counter *pc)
{
if(pc->address_mode == 1 && pc->offset == 1)
{
pc->offset = 0;
return;
}
if(pc->current_address > 0)
{
if(pc->address_mode == 1)
pc->offset = 1;
pc->current_address--;
}
else
fprintf(stderr,"Program counter reached beginning of memory");
}
void jump_counter(struct program_counter *pc, int address)
{
pc->offset = 0;
if(address > -1 && address < 4096)
pc->current_address = address;
else
fprintf(stderr,"Program counter jump address out of range");
}
void init_program_counter(struct program_counter * pc, int Machine_Memory[MEMORY_SIZE][WORD_SIZE])
{
set_address(pc,0,Machine_Memory);
clear_accumulator(pc);
clear_multiplier_quotient(pc);
compute_instruction(pc, Machine_Memory);
pc->address_mode = 0;
}
void clear_accumulator(struct program_counter * pc)
{
for(int i=0; i<ACCUMULATOR_SIZE; i++)
pc->accumulator[i] = 0;
}
void clear_multiplier_quotient(struct program_counter * pc)
{
for(int i=0; i<MULTIPLIER_QUOTIENT_SIZE; i++)
pc->multiplier_quotient[i] = 0;
}
void compute_instruction(struct program_counter *pc, int Machine_Memory[MEMORY_SIZE][WORD_SIZE])
{
if(pc->offset == 0)
{
for(int i=INSTRUCTION_SIZE-1; i>=0; i--)
{
pc->instruction[i] = Machine_Memory[pc->current_address][i];
}
}
else
{
for(int i=INSTRUCTION_SIZE-1; i>=0; i--)
{
pc->instruction[i] = Machine_Memory[pc->current_address][i+INSTRUCTION_SIZE];
}
}
}
int get_address(struct program_counter *pc)
{
int address = 0;
int pow = 1;
for(int i=OPCODE_SIZE; i<INSTRUCTION_SIZE; i++)
{
address += pc->instruction[i]*pow;
pow*=2;
}
return address;
}
void set_address(struct program_counter *pc, int address, int Machine_Memory[MEMORY_SIZE][WORD_SIZE])
{
pc->current_address = address;
for(int i=0; i<WORD_SIZE; i++)
pc->address_word[i] = Machine_Memory[address][i];
compute_instruction(pc,Machine_Memory);
}
int strip_opcode(struct program_counter *pc)
{
int pow = 1;
int output = 0;
int offset = pc->offset ? INSTRUCTION_SIZE : 0;
for(int i=0; i<OPCODE_SIZE; i++)
{
output+= pow*pc->instruction[i+offset];
pow*=2;
}
return output;
}
int accumulator_overflow(struct program_counter *pc)
{
// P and Q overflow bits
if(pc->accumulator[ACCUMULATOR_SIZE-3] == 1 || pc->accumulator[ACCUMULATOR_SIZE-2] == 1)
return 1;
return 0;
}
int accumulator_empty(struct program_counter *pc)
{
int is_positive = 1;
int is_zero = 1;
if(pc->accumulator[ACCUMULATOR_SIZE-1] == 1)
is_positive*=-1;
for(int i=0; i<ACCUMULATOR_SIZE-1; i++)
if(pc->accumulator[i] == 1)
{
is_zero = 0;
break;
}
if(is_zero)
return 0;
return is_positive;
}
long get_accumulator_value(struct program_counter *pc)
{
int value[ACCUMULATOR_SIZE];
//value[ACCUMULATOR_SIZE] = pc->accumulator[ACCUMULATOR_SIZE-1];
for(int i=0; i<ACCUMULATOR_SIZE; i++)
value[i] = pc->accumulator[i];
return signed_byte_value(value, ACCUMULATOR_SIZE-2);
}
void set_accumulator_value(struct program_counter *pc, long new_value)
{
int *temp_val = create_byte_value(new_value,ACCUMULATOR_SIZE);
for(int i=0; i<ACCUMULATOR_SIZE; i++)
pc->accumulator[i] = temp_val[i];
// memcpy(pc->accumulator,temp_val,ACCUMULATOR_SIZE*sizeof(int));
free(temp_val);
}
void print_pc(struct program_counter pc)
{
printf("Current Address: %d\n", pc.current_address);
printf("Offset: %d\n", pc.offset);
char* address_word = malloc(WORD_SIZE);
byte_value_to_string(pc.address_word, address_word, WORD_SIZE);
printf("Address Word: %s\n", address_word);
char* instruction_string = malloc(INSTRUCTION_SIZE);
byte_value_to_string(pc.instruction, instruction_string, INSTRUCTION_SIZE);
printf("Instruction: %s\n", instruction_string);
byte_value_to_string(pc.multiplier_quotient, address_word, WORD_SIZE);
printf("Multiplier Quoticent: %s\n", address_word);
char* accumulator_string = (char*) malloc(ACCUMULATOR_SIZE);
byte_value_to_string(pc.accumulator, accumulator_string, ACCUMULATOR_SIZE);
printf("Accumulator: %s\n", accumulator_string);
printf("Accumulator Int: %lld\n",signed_byte_value(pc.accumulator,ACCUMULATOR_SIZE));
printf("Multiplier Qotient: %lld\n",signed_byte_value(pc.multiplier_quotient, MULTIPLIER_QUOTIENT_SIZE));
printf("Combined Int: %lld\n\n",compute_multiplier_accumulator(&pc));
free(address_word);
free(instruction_string);
}
void toggle_address_mode(struct program_counter *pc)
{
pc->address_mode = !pc->address_mode;
}
/*void refresh_pc(struct program_counter *pc, int Machine_Memory[MEMORY_SIZE][WORD_SIZE])
{
compute_instruction(pc, Machine_Memory);
}*/