summaryrefslogtreecommitdiffstats
path: root/memedit.y
blob: 35d785082077d17031cdf18c4b4c465293869b43 (plain)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* FPGAedit */

%{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

extern int errno;
extern int fd;
extern int debug, mmaped_access, readback;
extern void *mem;
unsigned int cur_position;

typedef struct variable {
	struct variable *next;
	char *name;
	int val;
} variable;

unsigned long map_ofs = 0;
unsigned long map_size = 0;

int in_line = 0;
#define LINELEN 16
int write_len;
variable *vlist;

void set_variable (char *param, int val);
int get_variable (char *param);
int hex_output (unsigned long offset, unsigned long len, unsigned long count);
int fill_mem (int offset, int len, int count, int val);
void print_help (void);

extern void yyerror (const char *s);
extern int zzlex (void);
int yylex (void);
%}

%union {
long val;
char *tptr;
}

%left <val> NUM
%left <val> ADD
%left <val> SUB
%left <val> MUL
%left <val> DIV
%left <val> NEG
%left <val> MOD
%left <val> RSHIFT
%left <val> LSHIFT
%left <val> OBRA
%left <val> CBRA
%left <tptr> FILENAME
%left <tptr> VARNAME
%left <val> OPEN
%left <val> CLOSE
%left <val> MAP
%left <val> UNMAP
%left <val> MD
%left <val> MM
%left <val> MF
%left <val> EQ
%left <val> AT
%token <val> EXIT
%token <val> HELP
%token <val> ENV
%right <val> CR

%type <val> num
%type <val> filename

%% /* FPGAedit Grammar */

input:	  /* empty */
	| input line
;

line:	  CR
	| open CR
	| close CR
	| map CR
	| UNMAP CR {if (mmaped_access) munmap (mem, map_size);}
	| EXIT CR { exit(EXIT_SUCCESS); }
	| HELP CR { print_help(); }
	| ENV CR {
		variable *l;
		for (l = vlist; l; l = l->next)
			printf ("%s = 0x%x\n", l->name,l->val);
	}
	| VARNAME CR { printf ("%s = %d\n", $1, get_variable($1)); free($1); }
	| mem CR { printf ("\n"); }
	| VARNAME EQ num CR { set_variable ($1, $3); free($1); }
	| VARNAME EQ MD num CR {
		if (mmaped_access) {
			printf ("mmaped access: not implemented\n");
		}
		else if (fd) 
		{
			if (lseek (fd, $4, SEEK_SET) == -1)
			    printf ("set offset %ld failed\n", $4);
			else 
			{
				unsigned int c = 0;
				
				read(fd, &c, $3);
				set_variable ($1, c); 
				free($1);
			}
		}
		else printf ("please open file\n");
	}
	| error
;

open:	  OPEN { printf ("open <filename>\n"); }
	| OPEN filename {
		char *filename = (char *)$2;
	
		if (fd>=0)
			close (fd);
		fd = open(filename, O_RDWR | O_SYNC); 
		if (fd<0) 
			printf ("open >%s< failed\n%s\n", filename, strerror(errno));
		 
		free (filename);
	}
;

filename: FILENAME 
	| VARNAME

close:	  CLOSE { 
		if (fd>0) {
			close (fd); fd = -1;
			mmaped_access = 0;
		}
	}
;

map:      MAP { printf("map <start> <size>\n"); }
	| MAP num num {
		if (fd>0) {
	       		printf("mapping offset 0x%08lx (size 0x%lx)\n",$2,$3);
	       		map_ofs = $2;
	       		map_size = $3;
	       		mem = mmap(0, $3, PROT_READ | PROT_WRITE,
	       			MAP_SHARED, fd, $2);
       			if(mem == MAP_FAILED) {
       			       	  perror("mmap");
       			}
       			cur_position = 0;
			mmaped_access = 1;
		} else
			printf("load a file first\n");
	}
;

mem:	  MD { 
		if (mem)
			cur_position = hex_output (cur_position, $1, 16);
		else
			printf ("no region mapped\n");
	  }
	| MD num {
		if (mem) {
			cur_position = hex_output ($2, $1, 16);
		} else
			printf ("no region mapped\n");
	}
	| MD num num {
		if (mem)
			cur_position = hex_output ($2, $1, $3);
		else
			printf ("no region mapped\n");
	}
	| MD AT num {
		if (mem) 
			cur_position = hex_output ($3, $1, 16);
		else
			printf ("no region mapped\n");
	}
	| MD AT num num { 
		if (mem) 
			cur_position = hex_output ($3, $1, $4);
		else
			printf ("no region mapped\n");
	}
	| MF num num num {
		if (mem)
			fill_mem($2, $1, $3, $4);
		else
			printf ("no region mapped\n");
	}
	| MM { printf ("mm <offset> <value1> [<value2> ...]\n"); }
	| mm exp
;

mm:	MM { write_len = $1; if (debug > 3) printf ("write_len = %d\n", write_len);}
;

exp:	num {
		cur_position = $1;
		if (readback)
			printf ("new values:\n0x%08x: ",cur_position);
	}
	| exp num {
		unsigned int c = $2;
		if (mem) {
			switch (write_len) {
			case 1:
				c &= 0xff;
				*(volatile unsigned char *)(mem + cur_position) = c;
				if (readback)
					printf ("%02x ", *(volatile unsigned char *)(mem + cur_position));
				break;
			case 2:
				c &= 0xffff;
				*(volatile unsigned short *)(mem + cur_position) = c;
				if (readback)
					printf ("%04x ", *(volatile unsigned short *)(mem + cur_position));
				break;
			case 4:
				*(volatile unsigned long *)(mem + cur_position) = c;
				if (readback)
					printf ("%08lx ", *(volatile unsigned long *)(mem + cur_position));
				break;
			}
			cur_position += write_len;
		}
		else printf ("no region mapped\n");
	}
;

num:	  NUM
	| num ADD num { $$ = $1 + $3; if (debug > 3) printf ("ADD: %ld\n", $$); }
	| num SUB num { $$ = $1 - $3; if (debug > 3) printf ("SUB: %ld\n", $$); }
	| num MUL num { $$ = $1 * $3; if (debug > 3) printf ("MUL: %ld\n", $$); }
	| num DIV num { $$ = $1 / $3; if (debug > 3) printf ("DIV: %ld\n", $$); }
	| num MOD num { $$ = $1 % $3; if (debug > 3) printf ("MOD: %ld\n", $$); }
	| num RSHIFT num { $$ = $1 >> $3; if (debug > 3) printf ("RSHIFT: %ld\n", $$); }
	| num LSHIFT num { $$ = $1 << $3; if (debug > 3) printf ("LSHIFT: %ld\n", $$); }
	| OBRA num CBRA { $$ = $2; if (debug > 3) printf ("BRA: %ld\n", $$); }
	| VARNAME { $$ = get_variable ($1); if (debug > 3) printf ("VAR: %ld\n", $$); free($1); }
;

%%
void print_help (void) {
	printf ("\n");
	printf ("open <file>			open a file\n");
	printf ("close				close currently opened file\n");
	printf ("map <start> <size>		map memory starting at physical	address <start>\n");
	printf ("                  		into working buffer\n");
	printf ("exit, quit			quit\n");
	printf ("help				show this help text\n");
	printf ("\n");
	printf ("md [[@]<start> [<length>]]	mem display from current position\n");
	printf ("				or from <start> for <length>\n");
	printf ("mm <offset> <val1> .. <valN>	modify from value <offset>\n");
	printf ("mf <start> <length> <value>	fill from <offset> for <length> with <value>\n");
	printf ("env				print all currently defined variables\n");
	printf ("readback <val>			if true (default), read back values after write operation\n");
	printf ("				disable to avoid side-effects (e.g. for irq-registers)\n");
	printf ("<var> = <expr>			set variable <var> to <expr>\n");
	printf ("<var>				print variable <var>\n");
	printf ("\n");
	printf ("<var1> add <var2>		add variables\n");
	printf ("<var1> sub <var2>		substract variables\n");
	printf ("<var1> mul <var2>		multiplicate variables\n");
	printf ("<var1> div <var2>		divide variables\n");
	printf ("<var1> rshift <var2>		shift (or >>)\n");
	printf ("<var1> lshift <var2>		shift (or >> or shift)\n");
	printf ("\n");
	printf ("Variables can be entered as decimals (without prefix), hex (prefixed with 0x)\n");
	printf ("oct (0o) or bin (0b). For mm and md byte, word and long word access can be \n");
	printf ("enabled with .b, .w and .l\n");
	printf ("\n");
}

void set_variable (char *param, int val) {
	variable *l, *tmp;

	if (strncmp (param, "readback", 8) == 0) {
		readback = val;
		printf ("readback = %d\n", readback);
		return;
	}

	if (strncmp (param, "debug", 5) == 0) {
		debug = val; 
		printf ("debug = %d\n", debug);
		return;
	}
	
	if (debug > 3) printf ("param = >%s<\n", param);
	
	for (l = vlist; l; l = l->next) {
		if (debug > 3) printf ("l->name = >%s<\n", l->name);
		if (strcmp (l->name, param) == 0) {
			l->val = val;
			return;
		}
	}
	
	tmp = calloc (1, sizeof(variable));
	if (!tmp) {
		printf ("no mem for variable %s!\n", param);
		return;
	}
	
	tmp->name = calloc(1 + strlen(param), sizeof(char));
	if (!tmp->name) {
		printf ("no mem for variable %s!\n", param);
		free(tmp);
		return;
	}
	strcpy(tmp->name, param);
	tmp->val = val;
	tmp->next = vlist;
	
	vlist = tmp;
}

int get_variable (char *param) {
	int v = 0;
	variable *l;
	
	if (debug > 3) printf ("param = >%s<\n", param);
	
	for (l = vlist; l; l = l->next) {
		if (debug > 3) printf ("l->name = >%s<\n", l->name);
		if (strcmp (l->name, param) == 0) {
			v = l->val;
			break;
		}
	}
	return v;
}

int hex_output (unsigned long offset, unsigned long len, unsigned long count) {
	unsigned long c, j, t, bytes;
	unsigned char linebuf[LINELEN];

	if (fd<0) {
		printf ("please open file\n");
		return 0;
	}

	if( offset > map_size ) {
		printf("region not mapped\n");
		return -1;
	}

	if( offset + count > map_size )
		count = map_size - offset;
	
	while(1) {
		unsigned char *linechar = (unsigned char *)linebuf;
		unsigned int *lineint = (unsigned int *)linebuf;
		unsigned short *lineshort = (unsigned short *)linebuf;
		
		volatile unsigned int *rbuff32 = mem + offset;
		volatile unsigned short *rbuff16 = mem + offset;
		volatile unsigned char *rbuff8 = mem + offset;

		printf ("%08lx: ", offset);
		bytes = LINELEN > count ? count : LINELEN;
		if( bytes + offset > map_size )
			bytes = map_size - offset;

		for( t=bytes; t>0; t-=len) {
			switch (len) {
			case 1:
				c = *rbuff8++;
				printf ("%02lx ", c);
				*linechar++ = c;
				break;
			case 2:
				c = *rbuff16++;
				printf ("%04lx ", c);
				*lineshort++ = c;
				break;
			case 4:
				c = *rbuff32++;
				printf ("%08lx ", c);
				*lineint++ = c;
				break;
			}
		}
		for (j = 0; j < bytes; j++) {
			if(isprint(linebuf[j]))
				printf("%c",linebuf[j]);
			else
				printf(".");
		}
		printf ("\n");

		offset += bytes;
		count -= bytes;

		if(!count || offset == map_size)
			return offset;
	}
}

int fill_mem (int offset, int len, int count, int val) {

	int i;

	for (i = offset; i < offset + count; i += len) {
		switch (len) {
		case 1:
			val &= 0xff;
			*(volatile unsigned char *)(mem + i) = val;
			break;
		case 2:
			val &= 0xffff;
			*(volatile unsigned short *)(mem + i) = val;
			break;
		case 4:
			*(volatile unsigned long *)(mem + i) = val;
			break;
		}

	}

	return 0;
}

int yylex (void) {
	in_line++;
	return zzlex ();
}