forked from BarclayII/power86
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.c
97 lines (91 loc) · 2.31 KB
/
code.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
#include "code.h"
#include "mem.h"
#include "msg.h"
/* Read a single instruction from file and packup */
Ycode *y_code_read(FILE *f)
{
Ycode * c;
Yaddr a;
int i;
size_t x;
char s[12];
while (fscanf(f, "%i:%s", &a, s) < 2 || s[0] == '|'){
while (fgetc(f) != '\n'){
if (feof(f)) return NULL;
}
}
/* Allocate memory and pack instruction */
c = (Ycode *)malloc(sizeof(Ycode));
c -> addr = a;
c -> count = strlen(s) >> 1;
c -> comment = NULL;
for (i = 0; i < c -> count; ++ i)
sscanf(s + (i << 1), "%2hhx", &(c -> ins[i]));//WTF????
/* Skip to '|' */
while (fgetc(f) != '|');
ungetc('|', f);
/* Read comment */
x = 0;
i = getline(&(c -> comment), &x, f);
if (c -> comment[i - 1] == '\n') c -> comment[i - 1] = '\0';
return c;
}
/* Generate a new codelist from an opened file */
Ycodelist *y_code_new(FILE *f)
{
Ycodelist * head;
Ycodelist * tail;
Ycode * c;
c = y_code_read(f);
if (!c) return NULL;
head = (Ycodelist *)malloc(sizeof(Ycodelist));
tail = head;
tail -> code = c;
tail -> next = NULL;
while ((c = y_code_read(f))){
tail -> next = (Ycodelist *)malloc(sizeof(Ycodelist));
tail = tail -> next;
tail -> code = c;
tail -> next = NULL;
}
return head;
}
/* Dump code as debug message */
void y_code_dump(Ycodelist *l)
{
Ycode * c;
int i;
y_msg(Y_MSG_DEBUG, "Code dump:\n");
while (l){
c = l -> code;
l = l -> next;
y_msg_f(Y_MSG_DEBUG, "0x%08X: ", c -> addr);
for (i = 0; i < c -> count; ++ i) y_msg_f(Y_MSG_DEBUG, "%02X ", c -> ins[i]);
for (i = c -> count; i < 6; ++ i) y_msg_f(Y_MSG_DEBUG, " ");
y_msg_f(Y_MSG_DEBUG, "\t%s\n", c -> comment);
}
}
/* Free a codelist and all its components*/
void y_code_free(Ycodelist *l)
{
Ycodelist * tmp;
while (l){
tmp = l;
l = l -> next;
free(tmp -> code -> comment);
free(tmp -> code);
free(tmp);
}
}
/* Map a codelist into memory */
void y_code_map(Ycodelist *l)
{
Ycode * c;
int i;
while (l != NULL){
c = l -> code;
l = l -> next;
for (i = 0; i < c -> count; ++ i)
y_mem_write_byte(c -> addr + (Yaddr)i, c -> ins[i]);
}
}