From ac95818616e60c2cd19dbcc659c580f262b80299 Mon Sep 17 00:00:00 2001 From: Robert Schwebel Date: Tue, 11 Jan 2005 16:34:58 +0000 Subject: renamed to memedit --- Makefile.am | 24 ++-- configure.ac | 4 +- fpgaedit.c | 56 --------- fpgaedit.l | 252 ------------------------------------- fpgaedit.y | 360 ----------------------------------------------------- fpgaedit_parser.gp | 3 - memedit.c | 56 +++++++++ memedit.l | 252 +++++++++++++++++++++++++++++++++++++ memedit.y | 360 +++++++++++++++++++++++++++++++++++++++++++++++++++++ memedit_parser.gp | 3 + 10 files changed, 685 insertions(+), 685 deletions(-) delete mode 100644 fpgaedit.c delete mode 100644 fpgaedit.l delete mode 100644 fpgaedit.y delete mode 100644 fpgaedit_parser.gp create mode 100644 memedit.c create mode 100644 memedit.l create mode 100644 memedit.y create mode 100644 memedit_parser.gp diff --git a/Makefile.am b/Makefile.am index 58134fa..bc78825 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,26 +1,26 @@ -bin_PROGRAMS = fpgaedit -fpgaedit_SOURCES = fpgaedit_parser.c fpgaedit_parser.h \ - y.tab.c y.tab.h lex.zz.c fpgaedit.c +bin_PROGRAMS = memedit +memedit_SOURCES = memedit_parser.c memedit_parser.h \ + y.tab.c y.tab.h lex.zz.c memedit.c -fpgaedit_CFLAGS = -W -Wall +memedit_CFLAGS = -W -Wall -fpgaedit_LDADD = $(READLINE_LIBS) +memedit_LDADD = $(READLINE_LIBS) # only needed to recompile the commandlineparser -fpgaedit_parser.c fpgaedit_parser.h: fpgaedit_parser.gp - genparse -o fpgaedit_parser fpgaedit_parser.gp +memedit_parser.c memedit_parser.h: memedit_parser.gp + genparse -o memedit_parser memedit_parser.gp -y.tab.c y.tab.h: fpgaedit.y +y.tab.c y.tab.h: memedit.y $(YACC) -d $< -lex.zz.c: fpgaedit.l +lex.zz.c: memedit.l $(LEX) $< DISTCLEANFILES = *~ y.output y.tab.c y.tab.h lex.zz.c MAINTAINERCLEANFILES = aclocal.m4 compile config.guess config.sub \ configure depcomp install-sh ltmain.sh \ - Makefile.in missing fpgaedit_parser.c \ - fpgaedit_parser.h fpgaedit*.tar.gz config.h.in + Makefile.in missing memedit_parser.c \ + memedit_parser.h memedit*.tar.gz config.h.in -EXTRA_DIST = bootstrap fpgaedit_parser.gp fpgaedit.l fpgaedit.y +EXTRA_DIST = bootstrap memedit_parser.gp memedit.l memedit.y diff --git a/configure.ac b/configure.ac index 71bd767..c321103 100644 --- a/configure.ac +++ b/configure.ac @@ -2,9 +2,9 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ(2.57) -AC_INIT(fpgaedit, 0.2, b.spranger@pengutronix.de) +AC_INIT(memedit, 0.2, b.spranger@pengutronix.de) AM_INIT_AUTOMAKE -AC_CONFIG_SRCDIR([fpgaedit.c]) +AC_CONFIG_SRCDIR([memedit.c]) AC_CONFIG_HEADER([config.h]) # Checks for programs. diff --git a/fpgaedit.c b/fpgaedit.c deleted file mode 100644 index 1403e80..0000000 --- a/fpgaedit.c +++ /dev/null @@ -1,56 +0,0 @@ -/* - * $Id$ - * - */ - -#include -#include -#include -#include -#include - -extern int errno; -extern int yyparse(); - -#include "config.h" -#include "fpgaedit_parser.h" - -int debug = 0; -FILE *rl_instream; -FILE *fhan = NULL; - -int main (int argc, char *argv[]) -{ - struct arg_t *kom_arg; - - kom_arg = Cmdline (argc, argv); - - printf("\nfpgaedit - quick change of FPGA content\n"); - printf("Copyright (C) 2004 by Pengutronix\n"); - printf("Please enter 'help' for a list of commands. \n\n"); - - if (kom_arg->v) - { - printf ("%s: version %s\n", argv[0], PACKAGE_VERSION); - return EXIT_FAILURE; - } - - debug = kom_arg->V; - - if (kom_arg->optind) - { - fhan = fopen(argv[kom_arg->optind], "r+"); - if (!fhan) printf ("opening \"%s\" failed\n%s\n", - argv[kom_arg->optind], strerror(errno)); - } - - rl_instream = stdin; - yyparse (); - - return EXIT_SUCCESS; -} - -void yyerror (const char *s) /* Called by yyparse on error */ -{ - printf ("%s\n", s); -} diff --git a/fpgaedit.l b/fpgaedit.l deleted file mode 100644 index 3fa967c..0000000 --- a/fpgaedit.l +++ /dev/null @@ -1,252 +0,0 @@ -/* - * $Id$ - */ - -%option nostack -%option nounput -%option noyywrap -%option pointer -%option nodefault -%option case-insensitive -%option prefix="zz" - -%{ -#include -#include -#include -#include - -/* need this for the call to atol() below */ -#include -#include "y.tab.h" - -extern int debug; -extern int in_line; - -extern void yyerror(const char *); - -/* Have input call the following function. */ -#undef YY_INPUT -#define YY_INPUT(buf,result,max_size) \ - rl_input((char *)buf, &result, max_size) - -/* Variables to help interface readline with bc. */ -static char *rl_line = (char *)NULL; -static char *rl_start = (char *)NULL; -static int rl_len = 0; - -/* Definitions for readline access. */ -extern FILE *rl_instream; - -/* rl_input puts upto MAX characters into BUF with the number put in - BUF placed in *RESULT. If the yy input file is the same as - rl_instream (stdin), use readline. Otherwise, just read it. -*/ - -static void -rl_input (buf, result, max) - char *buf; - int *result; - int max; -{ - if (yyin != rl_instream) { - while ( (*result = read( fileno(yyin), buf, max )) < 0 ) - if (errno != EINTR) { - yyerror( "read() in flex scanner failed" ); - exit (1); - } - return; - } - - /* Do we need a new string? */ - if (rl_len == 0) { - if (rl_start) - free(rl_start); - rl_start = readline ("->"); - if (rl_start == NULL) { - /* end of file */ - *result = 0; - rl_len = 0; - return; - } - rl_line = rl_start; - rl_len = strlen (rl_line)+1; - if (rl_len != 1) - add_history (rl_line); - rl_line[rl_len-1] = '\n'; - fflush (stdout); - } - - if (rl_len <= max) { - strncpy (buf, rl_line, rl_len); - *result = rl_len; - rl_len = 0; - } else { - strncpy (buf, rl_line, max); - *result = max; - rl_line += max; - rl_len -= max; - } -} - -%} - -DIGIT [0-9] -ID [a-z][a-z0-9]* -HEX [0-9a-z] -OCT [0-7] -BIN [01] -%% - -q | -x | -exit | -quit { - if (debug > 4) printf ("EXIT\n"); - return EXIT; -} - -h | -help { - if (debug > 4) printf ("HELP\n"); - return HELP; -} - -m[dm](\.[biw])* { - if (strlen (yytext) == 2) yylval.val = 4; - else switch (*(yytext + 3)) { - case 'b': yylval.val = 1; break; - case 'i': yylval.val = 4; break; - case 'w': yylval.val = 2; break; - } - - if (*(yytext + 1) == 'd') { - if (debug > 4) printf ("MD %ld-bit\n",yylval.val*8); - return MD; - } - else { - if (debug > 4) printf ("MM %ld-bit\n",yylval.val*8); - return MM; - } -} - -\+ | -add { - if (debug > 4) printf ("ADD\n"); - return ADD; -} - -\- | -sub { - if (debug > 4) printf ("SUB\n"); - return SUB; -} - -\* | -mul { - if (debug > 4) printf ("MUL\n"); - return MUL; -} - -\/ | -div { - if (debug > 4) printf ("DIV\n"); - return DIV; -} - -\>\> | -rshift { - if (debug > 4) printf ("RSHIFT\n"); - return RSHIFT; -} - -\<\< | -shift | -lshift { - if (debug > 4) printf ("LSHIFT\n"); - return LSHIFT; -} - -\( { - if (debug > 4) printf ("OBRA\n"); - return OBRA; -} - -\) { - if (debug > 4) printf ("CBRA\n"); - return CBRA; -} - -\% | -mod { - if (debug > 4) printf ("MOD\n"); - return MOD; -} - -\= | -eq { - if (debug > 4) printf ("EQ\n"); - return EQ; -} - -\@ | -at { - if (debug > 4) printf ("AT\n"); - return AT; -} - -o | -open { - if (debug > 4) printf ("\n"); - return OPEN; -} - -c | -close { - if (debug > 4) printf ("\n"); - return CLOSE; -} - -{ID} { - yylval.tptr = malloc(sizeof(yytext)); - if (yylval.tptr) strcpy(yylval.tptr, yytext); - else printf ("no mem in %s", __FUNCTION__); - if (debug > 4) printf ("ID: >%s<\n", yytext); - return NAME; -} - -{DIGIT}+ { - yylval.val = atol(yytext); - if (debug > 4) printf ("DIGIT: %s (%ld)\n", yytext, yylval.val); - return NUM; -} - -0x{HEX}+ { - yylval.val = strtoul(yytext + 2, (char **)NULL, 16); - if (debug > 4) printf ("HEX: %s (%ld)\n", yytext, yylval.val); - return NUM; -} - -0o{OCT}+ { - yylval.val = strtoul(yytext + 2, (char **)NULL, 8); - if (debug > 4) printf ("OCT: %s (%ld)\n", yytext, yylval.val); - return NUM; -} - -0b{BIN}+ { - yylval.val = strtoul(yytext + 2, (char **)NULL, 2); - if (debug > 4) printf ("BIN: %s (%ld)\n", yytext, yylval.val); - return NUM; -} - -\n { - if (debug > 4) printf ("CR\n"); - in_line = 0; - return CR; -} - -. { - /* eat up any unmatched character */ - if (debug > 4) printf ("unmatched: 0x%02x\n", *yytext); -} -%% diff --git a/fpgaedit.y b/fpgaedit.y deleted file mode 100644 index 07edb13..0000000 --- a/fpgaedit.y +++ /dev/null @@ -1,360 +0,0 @@ -/* FPGAedit */ - -%{ -#include -#include -#include -#include -#include - -extern int errno; -extern FILE *fhan; -extern int debug; - -typedef struct variable { - struct variable *next; - char *name; - int val; -} variable; - -int in_line = 0; -#define LINELEN 16 -int write_len; -variable *vlist; - -void set_variable (char *param, int val); -int get_variable (char *param); -void hex_output (int offset, int len, int count); -void print_help (void); - -extern void yyerror (const char *s); -extern int zzlex (void); -int yylex (void); -%} - -%union { -long val; -char *tptr; -} - -%left NUM -%left ADD -%left SUB -%left MUL -%left DIV -%left NEG -%left MOD -%left RSHIFT -%left LSHIFT -%left OBRA -%left CBRA -%left NAME -%left OPEN -%left CLOSE -%left MD -%left MM -%left EQ -%left AT -%token EXIT -%token HELP -%right CR - -%type num - -%% /* FPGAedit Grammar */ - -input: /* empty */ - | input line -; - -line: CR - | open CR - | close CR - | EXIT CR { exit(EXIT_SUCCESS); } - | HELP CR { print_help(); } - | NAME CR { printf ("%s = %d\n", $1, get_variable($1)); free($1); } - | mem CR { printf ("\n"); } - | NAME EQ num CR { set_variable ($1, $3); free($1); } - | NAME EQ MD num CR { - if (fhan) - { - if (fseek (fhan, $4, SEEK_SET) == -1) - printf ("set offset %ld failed\n", $4); - else - { - unsigned int c = 0; - - fread(&c, $3, 1, fhan); - set_variable ($1, c); - free($1); - } - } - else printf ("please open file\n"); - } - | error -; - -open: OPEN { printf ("open \n"); } - | OPEN NAME { - if (fhan) fclose (fhan); - fhan = fopen($2, "r+"); - if (!fhan) - printf ("open >%s< failed\n%s\n", $2, strerror(errno)); - free ($2); - } -; - -close: CLOSE { - if (fhan) { - fclose (fhan); fhan = NULL; - } - } -; - -mem: MD { - if (fhan) hex_output (ftell (fhan), $1, 0); - else printf ("please open file\n"); - } - | MD num { hex_output ($2, $1, 0); } - | MD num num { hex_output ($2, $1, ($3 - 1)/16); } - | MD AT num { - if (fhan) - { - if (fseek (fhan, $3, SEEK_SET) == -1) - printf ("set offset %ld failed\n", $3); - else - { - unsigned int c = 0; - - fread(&c, $1, 1, fhan); - hex_output (c, $1, 0); - } - } - else printf ("please open file\n"); - } - | MD AT num num { - if (fhan) - { - if (fseek (fhan, $3, SEEK_SET) == -1) - printf ("set offset %ld failed\n", $3); - else - { - unsigned int c = 0; - - fread(&c, $1, 1, fhan); - hex_output (c, $1, $4); - } - } - else printf ("please open file\n"); - } - | MM { printf ("mm [ ...]\n"); } - | mm exp -; - -mm: MM { write_len = $1; if (debug > 3) printf ("write_len = %d\n", write_len);} -; - -exp: num { - if (fhan) { - long offset; - - if (fseek (fhan, $1, SEEK_SET) == -1) - printf ("set offset %ld failed\n", $1); - else { - offset = ftell (fhan); - if (offset%write_len) { - offset -= offset%write_len; - printf ("alligned to 0x%08lx\n", - offset); - fseek (fhan, offset, SEEK_SET); - } - printf ("new values:"); - if (offset%(LINELEN*4)) - printf ("0x%08lx", offset); - } - } - else printf ("please open file\n"); - } - | exp num { - unsigned int c; - long offset; - - c = $2; - if (debug > 3) printf ("write_len = %d\nc = 0x%0x\n", - write_len, c); - - if (fhan) { - offset = ftell (fhan); - if (offset%LINELEN) printf (" "); - else printf ("\n0x%08lx ", offset); - fwrite (&c, write_len, 1, fhan); - fflush (fhan); - fseek(fhan, -write_len, SEEK_CUR); - fread(&c, write_len, 1, fhan); - switch (write_len) { - case 1: c &= 0xff; printf ("%02x", c); break; - case 2: c &= 0xffff; printf ("%04x", c); break; - case 4: printf ("%08x", c); break; - } - } - else if (debug > 3) printf ("fhan == NULL\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", $$); } - | NAME { $$ = get_variable ($1); if (debug > 3) printf ("VAR: %ld\n", $$); free($1); } -; - -%% -void print_help (void) { - printf ("\n"); - printf ("open open a file\n"); - printf ("close close currently opened file\n"); - printf ("exit, quit quit\n"); - printf ("help show this help text\n"); - printf ("\n"); - printf ("md [[@] []] mem display from current position\n"); - printf (" or from to \n"); - printf ("mm .. modify from value \n"); - printf (" = set variable to \n"); - printf (" print variable \n"); - printf ("\n"); - printf (" add add variables\n"); - printf (" sub substract variables\n"); - printf (" mul multiplicate variables\n"); - printf (" div divide variables\n"); - printf (" rshift shift (or >>)\n"); - printf (" lshift 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, "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; -} - -void hex_output (int offset, int len, int count) { - unsigned int c; - int i=0, j, t; - unsigned char linebuf[LINELEN]; - - if (!fhan) { - printf ("please open file\n"); - return; - } - - if (fseek (fhan, offset, SEEK_SET) != 0) { - printf ("set offset %d failed\n", offset); - return; - } - - for (t = 0; t <= count; t++) { - unsigned int *lineint = (unsigned int *)linebuf; - unsigned short *lineshort = (unsigned short *)linebuf; - - printf ("%08x: ", offset + t*LINELEN); - switch (len) { - case 1: - for (i = 0; i < LINELEN; i++) { - c = 0; - if (fread (&c, 1, 1, fhan) != 1) break; - printf ("%02x ", c); - linebuf[i] = c; - } - break; - case 2: - for (i = 0; i < LINELEN; i+=2) { - c = 0; - if (fread (&c, 2, 1, fhan) != 1) break; - printf ("%04x ", c); - *lineshort++ = c; - } - break; - - case 4: - for (i = 0; i < LINELEN; i+=4) { - c = 0; - if (fread (&c, 4, 1, fhan) != 1) break; - printf ("%08x ", c); - *lineint++ = c; - } - break; - } - - for (j = 0; j < i; j++) { - if(isprint(linebuf[j])) - printf("%c",linebuf[j]); - else - printf("."); - } - printf ("\n"); - if(i < LINELEN) - return; - } -} - -int yylex (void) { - in_line++; - return zzlex (); -} diff --git a/fpgaedit_parser.gp b/fpgaedit_parser.gp deleted file mode 100644 index 74afb00..0000000 --- a/fpgaedit_parser.gp +++ /dev/null @@ -1,3 +0,0 @@ -/* fpgaedit_parser.gp */ -#mandatory fpgaedit -V / verbose int 0 "verbose output" diff --git a/memedit.c b/memedit.c new file mode 100644 index 0000000..5165f7d --- /dev/null +++ b/memedit.c @@ -0,0 +1,56 @@ +/* + * $Id$ + * + */ + +#include +#include +#include +#include +#include + +extern int errno; +extern int yyparse(); + +#include "config.h" +#include "memedit_parser.h" + +int debug = 0; +FILE *rl_instream; +FILE *fhan = NULL; + +int main (int argc, char *argv[]) +{ + struct arg_t *kom_arg; + + kom_arg = Cmdline (argc, argv); + + printf("\nmemedit - quick change of memory content\n"); + printf("Copyright (C) 2004 by Pengutronix\n"); + printf("Please enter 'help' for a list of commands. \n\n"); + + if (kom_arg->v) + { + printf ("%s: version %s\n", argv[0], PACKAGE_VERSION); + return EXIT_FAILURE; + } + + debug = kom_arg->V; + + if (kom_arg->optind) + { + fhan = fopen(argv[kom_arg->optind], "r+"); + if (!fhan) printf ("opening \"%s\" failed\n%s\n", + argv[kom_arg->optind], strerror(errno)); + } + + rl_instream = stdin; + yyparse (); + + return EXIT_SUCCESS; +} + +void yyerror (const char *s) /* Called by yyparse on error */ +{ + printf ("%s\n", s); +} diff --git a/memedit.l b/memedit.l new file mode 100644 index 0000000..3fa967c --- /dev/null +++ b/memedit.l @@ -0,0 +1,252 @@ +/* + * $Id$ + */ + +%option nostack +%option nounput +%option noyywrap +%option pointer +%option nodefault +%option case-insensitive +%option prefix="zz" + +%{ +#include +#include +#include +#include + +/* need this for the call to atol() below */ +#include +#include "y.tab.h" + +extern int debug; +extern int in_line; + +extern void yyerror(const char *); + +/* Have input call the following function. */ +#undef YY_INPUT +#define YY_INPUT(buf,result,max_size) \ + rl_input((char *)buf, &result, max_size) + +/* Variables to help interface readline with bc. */ +static char *rl_line = (char *)NULL; +static char *rl_start = (char *)NULL; +static int rl_len = 0; + +/* Definitions for readline access. */ +extern FILE *rl_instream; + +/* rl_input puts upto MAX characters into BUF with the number put in + BUF placed in *RESULT. If the yy input file is the same as + rl_instream (stdin), use readline. Otherwise, just read it. +*/ + +static void +rl_input (buf, result, max) + char *buf; + int *result; + int max; +{ + if (yyin != rl_instream) { + while ( (*result = read( fileno(yyin), buf, max )) < 0 ) + if (errno != EINTR) { + yyerror( "read() in flex scanner failed" ); + exit (1); + } + return; + } + + /* Do we need a new string? */ + if (rl_len == 0) { + if (rl_start) + free(rl_start); + rl_start = readline ("->"); + if (rl_start == NULL) { + /* end of file */ + *result = 0; + rl_len = 0; + return; + } + rl_line = rl_start; + rl_len = strlen (rl_line)+1; + if (rl_len != 1) + add_history (rl_line); + rl_line[rl_len-1] = '\n'; + fflush (stdout); + } + + if (rl_len <= max) { + strncpy (buf, rl_line, rl_len); + *result = rl_len; + rl_len = 0; + } else { + strncpy (buf, rl_line, max); + *result = max; + rl_line += max; + rl_len -= max; + } +} + +%} + +DIGIT [0-9] +ID [a-z][a-z0-9]* +HEX [0-9a-z] +OCT [0-7] +BIN [01] +%% + +q | +x | +exit | +quit { + if (debug > 4) printf ("EXIT\n"); + return EXIT; +} + +h | +help { + if (debug > 4) printf ("HELP\n"); + return HELP; +} + +m[dm](\.[biw])* { + if (strlen (yytext) == 2) yylval.val = 4; + else switch (*(yytext + 3)) { + case 'b': yylval.val = 1; break; + case 'i': yylval.val = 4; break; + case 'w': yylval.val = 2; break; + } + + if (*(yytext + 1) == 'd') { + if (debug > 4) printf ("MD %ld-bit\n",yylval.val*8); + return MD; + } + else { + if (debug > 4) printf ("MM %ld-bit\n",yylval.val*8); + return MM; + } +} + +\+ | +add { + if (debug > 4) printf ("ADD\n"); + return ADD; +} + +\- | +sub { + if (debug > 4) printf ("SUB\n"); + return SUB; +} + +\* | +mul { + if (debug > 4) printf ("MUL\n"); + return MUL; +} + +\/ | +div { + if (debug > 4) printf ("DIV\n"); + return DIV; +} + +\>\> | +rshift { + if (debug > 4) printf ("RSHIFT\n"); + return RSHIFT; +} + +\<\< | +shift | +lshift { + if (debug > 4) printf ("LSHIFT\n"); + return LSHIFT; +} + +\( { + if (debug > 4) printf ("OBRA\n"); + return OBRA; +} + +\) { + if (debug > 4) printf ("CBRA\n"); + return CBRA; +} + +\% | +mod { + if (debug > 4) printf ("MOD\n"); + return MOD; +} + +\= | +eq { + if (debug > 4) printf ("EQ\n"); + return EQ; +} + +\@ | +at { + if (debug > 4) printf ("AT\n"); + return AT; +} + +o | +open { + if (debug > 4) printf ("\n"); + return OPEN; +} + +c | +close { + if (debug > 4) printf ("\n"); + return CLOSE; +} + +{ID} { + yylval.tptr = malloc(sizeof(yytext)); + if (yylval.tptr) strcpy(yylval.tptr, yytext); + else printf ("no mem in %s", __FUNCTION__); + if (debug > 4) printf ("ID: >%s<\n", yytext); + return NAME; +} + +{DIGIT}+ { + yylval.val = atol(yytext); + if (debug > 4) printf ("DIGIT: %s (%ld)\n", yytext, yylval.val); + return NUM; +} + +0x{HEX}+ { + yylval.val = strtoul(yytext + 2, (char **)NULL, 16); + if (debug > 4) printf ("HEX: %s (%ld)\n", yytext, yylval.val); + return NUM; +} + +0o{OCT}+ { + yylval.val = strtoul(yytext + 2, (char **)NULL, 8); + if (debug > 4) printf ("OCT: %s (%ld)\n", yytext, yylval.val); + return NUM; +} + +0b{BIN}+ { + yylval.val = strtoul(yytext + 2, (char **)NULL, 2); + if (debug > 4) printf ("BIN: %s (%ld)\n", yytext, yylval.val); + return NUM; +} + +\n { + if (debug > 4) printf ("CR\n"); + in_line = 0; + return CR; +} + +. { + /* eat up any unmatched character */ + if (debug > 4) printf ("unmatched: 0x%02x\n", *yytext); +} +%% diff --git a/memedit.y b/memedit.y new file mode 100644 index 0000000..07edb13 --- /dev/null +++ b/memedit.y @@ -0,0 +1,360 @@ +/* FPGAedit */ + +%{ +#include +#include +#include +#include +#include + +extern int errno; +extern FILE *fhan; +extern int debug; + +typedef struct variable { + struct variable *next; + char *name; + int val; +} variable; + +int in_line = 0; +#define LINELEN 16 +int write_len; +variable *vlist; + +void set_variable (char *param, int val); +int get_variable (char *param); +void hex_output (int offset, int len, int count); +void print_help (void); + +extern void yyerror (const char *s); +extern int zzlex (void); +int yylex (void); +%} + +%union { +long val; +char *tptr; +} + +%left NUM +%left ADD +%left SUB +%left MUL +%left DIV +%left NEG +%left MOD +%left RSHIFT +%left LSHIFT +%left OBRA +%left CBRA +%left NAME +%left OPEN +%left CLOSE +%left MD +%left MM +%left EQ +%left AT +%token EXIT +%token HELP +%right CR + +%type num + +%% /* FPGAedit Grammar */ + +input: /* empty */ + | input line +; + +line: CR + | open CR + | close CR + | EXIT CR { exit(EXIT_SUCCESS); } + | HELP CR { print_help(); } + | NAME CR { printf ("%s = %d\n", $1, get_variable($1)); free($1); } + | mem CR { printf ("\n"); } + | NAME EQ num CR { set_variable ($1, $3); free($1); } + | NAME EQ MD num CR { + if (fhan) + { + if (fseek (fhan, $4, SEEK_SET) == -1) + printf ("set offset %ld failed\n", $4); + else + { + unsigned int c = 0; + + fread(&c, $3, 1, fhan); + set_variable ($1, c); + free($1); + } + } + else printf ("please open file\n"); + } + | error +; + +open: OPEN { printf ("open \n"); } + | OPEN NAME { + if (fhan) fclose (fhan); + fhan = fopen($2, "r+"); + if (!fhan) + printf ("open >%s< failed\n%s\n", $2, strerror(errno)); + free ($2); + } +; + +close: CLOSE { + if (fhan) { + fclose (fhan); fhan = NULL; + } + } +; + +mem: MD { + if (fhan) hex_output (ftell (fhan), $1, 0); + else printf ("please open file\n"); + } + | MD num { hex_output ($2, $1, 0); } + | MD num num { hex_output ($2, $1, ($3 - 1)/16); } + | MD AT num { + if (fhan) + { + if (fseek (fhan, $3, SEEK_SET) == -1) + printf ("set offset %ld failed\n", $3); + else + { + unsigned int c = 0; + + fread(&c, $1, 1, fhan); + hex_output (c, $1, 0); + } + } + else printf ("please open file\n"); + } + | MD AT num num { + if (fhan) + { + if (fseek (fhan, $3, SEEK_SET) == -1) + printf ("set offset %ld failed\n", $3); + else + { + unsigned int c = 0; + + fread(&c, $1, 1, fhan); + hex_output (c, $1, $4); + } + } + else printf ("please open file\n"); + } + | MM { printf ("mm [ ...]\n"); } + | mm exp +; + +mm: MM { write_len = $1; if (debug > 3) printf ("write_len = %d\n", write_len);} +; + +exp: num { + if (fhan) { + long offset; + + if (fseek (fhan, $1, SEEK_SET) == -1) + printf ("set offset %ld failed\n", $1); + else { + offset = ftell (fhan); + if (offset%write_len) { + offset -= offset%write_len; + printf ("alligned to 0x%08lx\n", + offset); + fseek (fhan, offset, SEEK_SET); + } + printf ("new values:"); + if (offset%(LINELEN*4)) + printf ("0x%08lx", offset); + } + } + else printf ("please open file\n"); + } + | exp num { + unsigned int c; + long offset; + + c = $2; + if (debug > 3) printf ("write_len = %d\nc = 0x%0x\n", + write_len, c); + + if (fhan) { + offset = ftell (fhan); + if (offset%LINELEN) printf (" "); + else printf ("\n0x%08lx ", offset); + fwrite (&c, write_len, 1, fhan); + fflush (fhan); + fseek(fhan, -write_len, SEEK_CUR); + fread(&c, write_len, 1, fhan); + switch (write_len) { + case 1: c &= 0xff; printf ("%02x", c); break; + case 2: c &= 0xffff; printf ("%04x", c); break; + case 4: printf ("%08x", c); break; + } + } + else if (debug > 3) printf ("fhan == NULL\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", $$); } + | NAME { $$ = get_variable ($1); if (debug > 3) printf ("VAR: %ld\n", $$); free($1); } +; + +%% +void print_help (void) { + printf ("\n"); + printf ("open open a file\n"); + printf ("close close currently opened file\n"); + printf ("exit, quit quit\n"); + printf ("help show this help text\n"); + printf ("\n"); + printf ("md [[@] []] mem display from current position\n"); + printf (" or from to \n"); + printf ("mm .. modify from value \n"); + printf (" = set variable to \n"); + printf (" print variable \n"); + printf ("\n"); + printf (" add add variables\n"); + printf (" sub substract variables\n"); + printf (" mul multiplicate variables\n"); + printf (" div divide variables\n"); + printf (" rshift shift (or >>)\n"); + printf (" lshift 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, "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; +} + +void hex_output (int offset, int len, int count) { + unsigned int c; + int i=0, j, t; + unsigned char linebuf[LINELEN]; + + if (!fhan) { + printf ("please open file\n"); + return; + } + + if (fseek (fhan, offset, SEEK_SET) != 0) { + printf ("set offset %d failed\n", offset); + return; + } + + for (t = 0; t <= count; t++) { + unsigned int *lineint = (unsigned int *)linebuf; + unsigned short *lineshort = (unsigned short *)linebuf; + + printf ("%08x: ", offset + t*LINELEN); + switch (len) { + case 1: + for (i = 0; i < LINELEN; i++) { + c = 0; + if (fread (&c, 1, 1, fhan) != 1) break; + printf ("%02x ", c); + linebuf[i] = c; + } + break; + case 2: + for (i = 0; i < LINELEN; i+=2) { + c = 0; + if (fread (&c, 2, 1, fhan) != 1) break; + printf ("%04x ", c); + *lineshort++ = c; + } + break; + + case 4: + for (i = 0; i < LINELEN; i+=4) { + c = 0; + if (fread (&c, 4, 1, fhan) != 1) break; + printf ("%08x ", c); + *lineint++ = c; + } + break; + } + + for (j = 0; j < i; j++) { + if(isprint(linebuf[j])) + printf("%c",linebuf[j]); + else + printf("."); + } + printf ("\n"); + if(i < LINELEN) + return; + } +} + +int yylex (void) { + in_line++; + return zzlex (); +} diff --git a/memedit_parser.gp b/memedit_parser.gp new file mode 100644 index 0000000..90cf91a --- /dev/null +++ b/memedit_parser.gp @@ -0,0 +1,3 @@ +/* memedit_parser.gp */ +#mandatory memedit +V / verbose int 0 "verbose output" -- cgit v1.2.3