-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.h
147 lines (120 loc) · 4.21 KB
/
sim.h
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
// Copyright (c) 2015, Sam Silberstein. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License").
// Author: [email protected]
#ifndef SIM_H
#define SIM_H
#include <stdio.h>
#include <setjmp.h>
#define TRUE 1
#define FALSE 0
/*
Instruction IDs for:
Halt, write, read boolean, read integer, jump if condition is true,
jump if condition not true, jump unconditional, move register to register,
move address to address, immediate data move, immediate data move w/ negative
number, load, store, register comparisons (equal to, not equal to, less than,
less than or equal to), numerical arithmetic (and, or, not, addition,
subtraction, multiplication, division, negation)
*/
enum {
HALT, WRITE, READB, READI, JUMPIF, JUMPNIF, JUMP,
MOVE, IDM, LOAD, STORE, EQ, NEQ, LT, LTE, AND, OR,
NOT, ADD, SUB, MULT, DIV, NEG
};
/*
Types of address values
*/
enum {
DISP, REG
};
/*
Types of arguments
*/
enum {
NUMBER, REGISTER, ADDRESS
};
/*
Types of stack_entrys
*/
enum {
INSTRUCTION, DATA
};
struct address{
int value;
unsigned int type;
};
struct argument {
int number;
unsigned int reg, addc, type;
struct address add[3];
};
struct stack_entry {
char *instruction;
int data;
unsigned int op, data_type, argc;
struct argument arguments[3];
};
#define MAX_SEGMENTS 16
#define MAX_REGISTERS 100
#define STACK_SIZE 256
struct breakpoint {
int id;
int enabled;
unsigned int addr;
struct breakpoint *next;
};
struct ami_machine {
/* debug options */
int opt_printstack;//for 'print' command with stack
int opt_dumpreg;//for 'print' command with registers
int opt_graphical;//to select graphical or text mode
char *filename;//holds the name of the input file
int opt_ac;//command line argument count
char **opt_av;//command line arguments
struct breakpoint *breakpoints;//list of breakpoints
/* gui management */
char *shm;//pointer to shared memory
int console_io_status;//holds status code for GUI console
int console_io_value;//holds value to pass between sim and
//GUI console
/* run state */
int halted;//halts the simulator after executing a 'halt' command
/* memory state */
struct stack_entry mem[STACK_SIZE];//virtual memory for
//instructions & data
unsigned int slots_used;//# of mem slots that are instructions
/* CPU registers */
int R[MAX_REGISTERS];//virtual registers
unsigned int PC, nPC;//program counter, next program counter
int reg_count;//count of registers (for printing)
};
struct ami_machine *create_ami_machine(void);
void allocate_stack(struct ami_machine *m);
void push_arguments(struct ami_machine *m);
void free_segments(struct ami_machine *m);
struct stack_entry *allocate_segment(struct ami_machine *m, unsigned int addr, unsigned int size, char *type);
void dump_segments(struct ami_machine *m);
void dump_registers(struct ami_machine *m);
void dump_stack(struct ami_machine *m, int start);
void dump_disassembly(FILE *out, unsigned int pc, unsigned int inst);
void dump_mem(struct ami_machine *m, unsigned int addr, int count, int size);
int arg_get_value(struct ami_machine *m, struct argument arg);
int add_get_value(struct ami_machine *m, struct argument add);
int mem_get_addr(struct ami_machine *m, struct argument arg);
int mem_read(struct ami_machine *m, unsigned int addr);
void mem_write(struct ami_machine *m, unsigned int addr, int value);
char *read_stack_entry(struct ami_machine *m, int addr);
char *readfile(char *filename);
struct stack_entry disasm_instr(struct ami_machine *m, char *instr);
char *read_argument(struct stack_entry *ret, char *token, char *stop_words[], int words);
void init_stop_words(char *stop_words[]);
enum { RUN_OK=0, RUN_BREAK=1, RUN_BREAKPOINT=2, RUN_FAULT=3, RUN_EXIT=4, RUN_HALTED=5 };
int run(struct ami_machine* m, int count);
void show_exit_status(struct ami_machine *m);
void update_gui(struct ami_machine *m);
void interactive_debug(struct ami_machine* m);
int is_breakpoint(struct ami_machine *m, unsigned int addr);
int dosyscall(struct ami_machine *m);
void raise(struct ami_machine *m, char *msg) __attribute__ ((noreturn));
extern jmp_buf err_handler;
#endif // SIM_H