1 /****************************************************************************
2     Copyright (C) 1987-2015 by Jeffery P. Hansen
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 
18     Last edit by hansen on Fri Jan  9 19:20:55 2009
19 ****************************************************************************/
20 #ifndef __gmac_h
21 #define __gmac_h
22 #include "config.h"
23 
24 #define MAXBLOCKS		128	/* Maximum number of code blocks */
25 #define MAXBANKS		128	/* Maximum number of banks */
26 #define WORDSPERLINE		8	/* Number of words to output per line */
27 
28 #define GE_OK			0	/* No error */
29 #define GE_IDRANGE		1	/* Negative or too large id number */
30 #define GE_REDECID		2	/* Redefined id number */
31 #define GE_UNDEFID		3	/* Undefined id number */
32 #define GE_BADTYPE		4	/* Reg used as value or vice versa */
33 
34 /*
35  * Output formats
36  */
37 #define OF_OLDGATE		0	/* Old TkGate 1.8 format */
38 #define OF_VERILOG		1	/* Standard verilog format */
39 
40 #define NMASK(n)	(((n) == 32) ? 0xffffffff : ((1 << (n))-1))
41 
42 #define new(t)		(t*)malloc(sizeof(t))
43 
44 /*
45  * A bank corresponds to a single RAM or ROM device in a tkgate circuit file.
46  * The msb and lsb are bit range from micro-word or macro-word which map to the
47  * device, and the address range specifies the portion of address space which
48  * will get mapped at address 0 on the specific device.
49  */
50 typedef struct {
51   char		*b_name;		/* Name of bank */
52   Range		b_range;		/* Range of bits */
53   int		b_start;		/* Starting address */
54   int		b_end;			/* Ending address */
55 } Bank;
56 
57 /*
58  * A memory instruction is an operation which modifies a memory location.
59  * Memory instructions are used as an intermediate form for generated code
60  * as well as to represent macro operations.
61  */
62 typedef struct meminst {
63   int		mi_addr;		/* Address of memory instruction */
64   Range		mi_sRange;		/* Bit range of source */
65   Range		mi_dRange;		/* Bit range of destination */
66   int		mi_valType;		/* Type of value to store */
67   int		mi_offset;		/* Offset for value (if needed) */
68   union {
69     int	d;				/* Numeric value */
70     char *s;				/* Symbolic value */
71   } mi_value;
72   struct meminst *mi_next;		/* Next instruction */
73 } MemInst;
74 
75 /*
76  * A memory program (MemProg) is a sequence of memory instructions.
77  */
78 typedef struct {
79   int		mb_nbits;		/* Number of bits in a word */
80   unsigned	mb_base;		/* Base address of block */
81   unsigned	mb_current;		/* Current address in block */
82   MemInst	*mb_first;		/* First memory instruction */
83   MemInst	*mb_last;		/* Last memory instruction */
84 } MemProg;
85 
86 /*
87  * Descriptor for a single operand.  od_type is an AM_* value.
88  *
89  * %1
90  * #2(%3)
91  */
92 typedef struct {
93   int		od_type;		/* Type of operand */
94   int		od_reg;			/* Id of register */
95   int		od_val;			/* Id of value */
96 } Opr;
97 
98 /*
99  * Describes an operation used to build a macro instruction.
100  *
101  * Examples:
102  *   +1[3:0]=1		type=OI_DATA
103  *   +0[3:0]=%2		type=OI_REGOP
104  *   +3[3:0]=#1[3:0]	type=OI_NUMOP
105  *   +3[3:0]=#1-@+2	type=OI_RELNUMOP
106  *
107  */
108 typedef struct {
109   int		oi_type;	/* Type of operation */
110   int		oi_base;	/* Base word */
111   Range		oi_dRange;	/* Bit range on destionation */
112   Range		oi_sRange;	/* Bit range on source */
113   int		oi_value;	/* Value or index */
114   int		oi_offset;	/* Offset for relative operands */
115 } OpInst;
116 
117 /*
118  * An OprDef represents a single set of operands and its memory program.
119  *
120  * %1,%2 = { +1[7:4]=%1, +0[3:0]=%2 }
121  */
122 typedef struct opr_def OprDef;
123 struct opr_def {
124   int		od_numOprs;		/* Number of operands */
125   Opr		**od_oprs;		/* Array of operands */
126 
127   int		od_numInsts;		/* Number of op instructions */
128   OpInst	**od_insts;		/* Array of op instructions */
129 
130   OprDef	*od_next;		/* Next operand */
131 };
132 
133 /*
134  * An OprGroup is an ordered list of operand sets (OprDef).
135  *
136  * operands foo {
137  *    %1,%2 = { +0[3:0]=0, +1[7:4]=%1, +1[3:0]=%2 };
138  *    %1,#2 = { +0[3:0]=5, +1[7:4]=%1, +2[7:0]=#2 };
139  * }
140  */
141 typedef struct {
142   char		*og_name;		/* Name of group */
143   int		og_nbits;		/* Bit width of macro memory */
144   int		og_numOdefs;		/* Number of operand defintions */
145   OprDef	**og_odefs;		/* Array of operand definitions */
146 } OprGroup;
147 
148 /*
149  * Definition of macro operation
150  */
151 typedef struct {
152   char		*od_name;		/* Name of operator */
153   int		od_numGroups;		/* Number of operand groups */
154   OprGroup	**od_groups;		/* Array of operand group.  [0] is local group */
155 
156   int		od_numInsts;		/* Number of top-level operator instuctions */
157   OpInst	**od_insts;		/* Top-level operator instuctions */
158 } OpDef;
159 
160 typedef struct {
161   char		*uf_name;		/* Name of field */
162   int		uf_isNegative;		/* Is this a negative field? */
163   int		uf_msb;			/* High bit of field */
164   int		uf_lsb;			/* Low bit of field */
165   SHash		uf_codes;		/* Symbolic values of field */
166 } UField;
167 
168 typedef struct {
169   int		c_type;			/* MACROCODE, MICROCODE, or MAP */
170   int		c_numBlocks;		/* Number of blocks */
171   MemProg	*c_blocks[MAXBLOCKS];	/* Array of code blocks */
172 
173   int		c_numBanks;		/* Number of banks */
174   Bank		*c_banks[MAXBANKS];	/* Array of banks */
175 
176   int		c_nbits;		/* Number of bits in micro-instruction */
177   SHash		c_labels;		/* Branch targets */
178 
179   SHash		c_ops;			/* Fields in the microcode, ops in macrocode */
180   SHash		c_oprs;			/* Operands for macrocode */
181 } Code;
182 
183 #define Bank_name(b)		(b)->b_name
184 #define Bank_msb(b)		(b)->b_range.msb
185 #define Bank_lsb(b)		(b)->b_range.lsb
186 #define Bank_start(b)		(b)->b_start
187 #define Bank_end(b)		(b)->b_end
188 #define Bank_noAddrRange(b)	((b)->b_start < 0)
189 
190 #define MemProg_current(mb)	(mb)->mb_current
191 #define MemProg_incAddr(mb)	(mb)->mb_current++
192 #define MemProg_start(mb)	(mb)->mb_base
193 #define MemProg_end(mb)	(mb)->mb_current
194 #define MemProg_firstMI(mb)	(mb)->mb_first
195 
196 #define MemInst_next(mi)	(mi)->mi_next
197 
198 #define UField_name(f)		(f)->uf_name
199 #define UField_msb(f)		(f)->uf_msb
200 #define UField_lsb(f)		(f)->uf_lsb
201 #define UField_findEnum(f,n)	((int*)SHash_find(&(f)->uf_codes,n))
202 
203 #define Code_nbits(c)		(c)->c_nbits
204 #define Code_findField(c,n)	((UField*)SHash_find(&(c)->c_ops,n))
205 #define Code_findLabel(c,n)	((int*)SHash_find(&(c)->c_labels,n))
206 #define Code_getMBlock(c,n)	(c)->c_blocks[(n)]
207 #define Code_numMBlocks(c)	(c)->c_numBlocks
208 
209 #define imin(a,b)	((a) < (b) ? (a) : (b))
210 #define imax(a,b)	((a) > (b) ? (a) : (b))
211 
212 #define isOverlap(s1,e1,s2,e2)	(s2 <= e1 && e2 >= s1)
213 
214 MemInst *new_MemInst(int addr,int dmsb,int dlsb);
215 void MemInst_setNumValue(MemInst*,int val);
216 void MemInst_setSymValue(MemInst*,char *val,int offset,int msb,int lsb);
217 void MemInst_print(MemInst *mi,FILE *f);
218 int MemInst_value(MemInst *mi);
219 
220 MemProg *new_MemProg(int,int);
221 void MemProg_addInst(MemProg*,MemInst*);
222 
223 Bank *new_Bank(char *name,int msb,int lsb,int start,int end);
224 
225 UField *new_UField(char *name,int msb,int lsb,int isComp);
226 
227 OpInst *new_OpInst(int otype,int base,int dmsb,int dlsb,int val,int smsb,int slsb,int offset);
228 MemInst *OpInst_genMemInst(OpInst*,MemProg*,int*,Number**);
229 
230 OprDef *new_OprDef(int);
231 void OprDef_addOpr(OprDef*,int,int,int);
232 void OprDef_addOpInst(OprDef*,OpInst*);
233 void OprDef_print(OprDef*,FILE*);
234 int OprDef_isMatch(OprDef*,int*,int);
235 int OprDef_check(OprDef*);
236 
237 OprGroup *new_OprGroup(char*,int);
238 OprDef *OprGroup_addOprDef(OprGroup *);
239 OprDef *OprGroup_findOpr(OprGroup*,int*,int);
240 
241 OpDef *new_OpDef(char*,int);
242 void OpDef_addOpInst(OpDef*,OpInst*);
243 void OpDef_addGroup(OpDef*,OprGroup*);
244 void OpDef_print(OpDef*,FILE*);
245 OprDef *OpDef_findOpr(OpDef*,int*,int);
246 
247 Code *new_Code(int);
248 void Code_addBank(Code *c,Bank *B);
249 void Code_addField(Code *c,UField *f);
250 void Code_addMemProg(Code *c,MemProg *mb);
251 void Code_addLabel(Code *c,char *lab,int addr);
252 void Code_generate(Code*,FILE*);
253 
254 void ParseFile(char *fileName);
255 
256 #endif
257 
258