lc3.h
Go to the documentation of this file.
1 #ifndef __LC3_H__
2 #define __LC3_H__
3 
4 /*
5  * "Copyright (c) 2014-2015 by Fritz Sieker."
6  * based on ideas/code by Steven S. Lumetta
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written
10  * agreement is hereby granted, provided that the above copyright notice
11  * and the following two paragraphs appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
14  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
21  * BASIS, AND THE AUTHOR NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
22  * UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  */
24 
34 #include <stdio.h>
35 #include <stdbool.h>
36 
37 #include "symbol.h"
38 
40 #ifndef LC3_VAR
41 #define LC3_VAR extern
42 #endif
43 
48 char* strdup(const char *);
49 
51 #define LC3_WORD unsigned short
52 
54 #define LC3_MEM_SIZE (1 << 16)
55 
57 #define LC3_NUM_REGS 8
58 
60 #define RETURN_ADDR_REG 7
61 
66 typedef enum opcode {
67  OP_INVALID = -1,
103 } opcode_t;
104 
112 typedef enum operand {
113  OPN_DR = 0x0001,
114  OPN_SR1 = 0x0002,
115  OPN_SR2 = 0x0004,
116  OPN_CC = 0x0008,
117  OPN_IMM5 = 0x0010,
118  OPN_OFF6 = 0x0020,
119  OPN_VEC8 = 0x0040,
120  OPN_ASC8 = 0x0080,
121  OPN_PCO9 = 0x0100,
122  OPN_PCO11 = 0x0200,
123  OPN_IMM16 = 0x0400,
124  OPN_STR = 0x0800,
125  OPN_FILL = 0x1000
126 } operand_t;
127 
140 typedef enum format {
141  FMT_ = 0,
142  FMT_RRR = (OPN_DR | OPN_SR1 | OPN_SR2),
143  FMT_RRI5 = (OPN_DR | OPN_SR1 | OPN_IMM5),
144  FMT_CL = (OPN_CC | OPN_PCO9),
145  FMT_R1 = OPN_DR,
146  FMT_R2 = OPN_SR1,
147  FMT_I11 = OPN_PCO11,
148  FMT_RL = (OPN_DR | OPN_PCO9),
149  FMT_RRI6 = (OPN_DR | OPN_SR1 | OPN_OFF6),
150  FMT_RR = (OPN_DR | OPN_SR1),
151  FMT_V = OPN_VEC8,
152  FMT_A = OPN_ASC8,
153  FMT_16 = OPN_IMM16,
154  FMT_S = OPN_STR,
155  FMT_FILL = OPN_FILL
156 } format_t;
157 
161 typedef struct inst_format {
162  char* name;
164  int prototype;
165 } inst_format_t;
166 
172 typedef struct LC3_inst {
173  int formBit;
183 } LC3_inst_t;
184 
189 char lc3_escaped_char (char c);
190 
198 
205 inst_format_t* lc3_get_inst_format (const char* name);
206 
212 char* lc3_get_suffix (char* file_name);
213 
219 bool lc3_file_has_suffix (const char* file_name, const char* suffix);
220 
227 char* lc3_replace_suffix (char* file_name, char* new_suffix);
228 
233 const char* lc3_get_format_name (format_t format);
234 
239 const char* lc3_get_opcode_name (opcode_t op);
240 
246 
251 void lc3_set_obj_file_mode (const char* name);
252 
257 int lc3_read_LC3_word (FILE *f);
258 
265 void lc3_read_sym_table (FILE* sym_file, sym_table_t* symTab);
266 
271 void lc3_write_LC3_word (FILE* f, int value);
272 
278 void lc3_write_sym_table (FILE* sym_file, sym_table_t* symTab);
279 
288 bool lc3_get_address (sym_table_t* symTab, const char* token, int* value);
289 
290 #endif /* __LC3_H__ */
291 
Definition: lc3.h:119
Definition: lc3.h:78
Definition: lc3.h:86
Definition: lc3.h:122
Definition: lc3.h:67
int formBit
Definition: lc3.h:173
Definition: lc3.h:90
Definition: lc3.h:113
Definition: lc3.h:93
Defines the interface to symbol.c functions (do not modify)
Definition: lc3.h:120
Definition: lc3.h:96
Definition: lc3.h:161
void lc3_write_sym_table(FILE *sym_file, sym_table_t *symTab)
Definition: lc3.c:248
Definition: lc3.h:118
char * lc3_replace_suffix(char *file_name, char *new_suffix)
Definition: lc3.c:138
char * lc3_get_suffix(char *file_name)
Definition: lc3.c:122
Definition: lc3.h:121
bool lc3_file_has_suffix(const char *file_name, const char *suffix)
Definition: lc3.c:131
Definition: lc3.h:72
Definition: lc3.h:76
Definition: lc3.h:70
enum operand operand_t
Definition: lc3.h:101
void lc3_read_sym_table(FILE *sym_file, sym_table_t *symTab)
Definition: lc3.c:220
Definition: lc3.h:102
Definition: lc3.h:117
void lc3_set_obj_file_mode(const char *name)
Definition: lc3.c:150
Definition: lc3.h:94
struct inst_format inst_format_t
Definition: lc3.h:74
LC3_inst_t * lc3_get_inst_info(opcode_t opcode)
Definition: lc3.c:93
opcode
Definition: lc3.h:66
bool lc3_get_address(sym_table_t *symTab, const char *token, int *value)
Definition: lc3.c:204
inst_format_t * lc3_get_inst_format(const char *name)
Definition: lc3.c:102
Definition: lc3.h:116
operand
Definition: lc3.h:112
Definition: lc3.h:172
Definition: lc3.h:99
Definition: lc3.h:125
Definition: lc3.h:115
Definition: lc3.h:124
enum opcode opcode_t
struct LC3_inst LC3_inst_t
Definition: lc3.h:69
format_t operands
Definition: lc3.h:163
Definition: lc3.h:79
Definition: lc3.h:95
const char * lc3_get_operand_name(operand_t operand)
Definition: lc3.c:345
Definition: lc3.h:100
char * strdup(const char *)
Definition: lc3.h:114
Definition: lc3.h:87
Definition: lc3.h:83
const char * lc3_get_opcode_name(opcode_t op)
Definition: lc3.c:298
Definition: lc3.h:123
inst_format_t forms[2]
Definition: lc3.h:178
char * name
Definition: lc3.h:162
Definition: lc3.h:98
enum format format_t
Definition: lc3.h:75
char lc3_escaped_char(char c)
Definition: lc3.c:188
Definition: lc3.h:71
Definition: lc3.h:68
format
Definition: lc3.h:140
void lc3_write_LC3_word(FILE *f, int value)
Definition: lc3.c:173
Definition: lc3.h:82
Definition: lc3.h:97
Definition: lc3.h:88
int prototype
Definition: lc3.h:164
Definition: lc3.h:73
Definition: lc3.h:85
Definition: symbol.c:35
const char * lc3_get_format_name(format_t format)
Definition: lc3.c:363
Definition: lc3.h:84
Definition: lc3.h:92
int lc3_read_LC3_word(FILE *f)
Definition: lc3.c:154
Definition: lc3.h:80
Definition: lc3.h:91
Definition: lc3.h:81
Definition: lc3.h:77