123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- #include <stdio.h>
- #include <ctype.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include "../tools/getoptv.h"
- #include "../tools/endian.h"
- #include "../tools/number.h"
- #include "../tools/chars.h"
- #include "../tools/error.h"
- #include "../tools/flags.h"
- #include "../mdio/mdio.h"
- #ifndef MAKEFILE
- #include "../tools/getoptv.c"
- #include "../tools/putoptv.c"
- #include "../tools/version.c"
- #include "../tools/error.c"
- #include "../tools/todigit.c"
- #endif
- static signed c;
- static unsigned row = 1;
- static unsigned col = 1;
- static uint16_t count = 0;
- static uint16_t instr = 0;
- static uint32_t addr;
- static uint32_t data;
- static uint32_t mask;
- static signed mygetc ()
- {
- extern unsigned row;
- extern unsigned col;
- signed c = getc (stdin);
- if (c == '\n')
- {
- row++;
- col = 0;
- }
- else if (~c)
- {
- col++;
- }
- return (c);
- }
- static uint32_t integer (unsigned radix)
- {
- uint32_t value = 0;
- unsigned digit = 0;
- while ((digit = todigit (c)) < radix)
- {
- value *= radix;
- value += digit;
- c = mygetc ();
- }
- while (isspace (c))
- {
- c = mygetc ();
- }
- return (value);
- }
- static void assemble (flag_t flags)
- {
- c = mygetc ();
- while (c != EOF)
- {
- if (isspace (c))
- {
- do
- {
- c = mygetc ();
- }
- while (isspace (c));
- continue;
- }
- if ((c == '#') || (c == ';'))
- {
- do
- {
- c = mygetc ();
- }
- while (nobreak (c));
- continue;
- }
- addr = integer (16);
- data = integer (16);
- mask = integer (16);
- instr = MDIO16_INSTR (1, 1, (0x03 << 3), 0, 2);
- write (STDOUT_FILENO, &instr, sizeof (instr));
- instr = HTOLE16 ((addr >> 9) & 0x03FF);
- write (STDOUT_FILENO, &instr, sizeof (instr));
- instr = HTOLE16 (0xFFFF);
- write (STDOUT_FILENO, &instr, sizeof (instr));
- count++;
- instr = MDIO16_INSTR (1, 1, (0x02 << 3) | ((MDIO32_LO_ADDR (addr) & 0xE0) >> 5), MDIO32_LO_ADDR (addr) & 0x1F, 2);
- write (STDOUT_FILENO, &instr, sizeof (instr));
- instr = HTOLE16 (data & 0xFFFF);
- write (STDOUT_FILENO, &instr, sizeof (instr));
- instr = HTOLE16 (mask & 0xFFFF);
- write (STDOUT_FILENO, &instr, sizeof (instr));
- count++;
- instr = MDIO16_INSTR (1, 1, (0x02 << 3) | ((MDIO32_LO_ADDR (addr) & 0xE0) >> 5), (MDIO32_LO_ADDR (addr) & 0x1F) | 0x01, 2);
- write (STDOUT_FILENO, &instr, sizeof (instr));
- instr = HTOLE16 ((data >> 16) & 0xFFFF);
- write (STDOUT_FILENO, &instr, sizeof (instr));
- instr = HTOLE16 ((mask >> 16) & 0xFFFF);
- write (STDOUT_FILENO, &instr, sizeof (instr));
- count++;
- if (_anyset (flags, MDIO_VERBOSE))
- {
- fprintf (stderr, "REG=0x%08X DATA=0x%08X MASK=0x%08X\n", addr, data, mask);
- }
- if ((c == ';') || (c == EOF))
- {
- c = mygetc ();
- continue;
- }
- if (_allclr (flags, MDIO_SILENCE))
- {
- error (1, 0, "Illegal character or missing terminator: line %d col %d", row, col);
- }
- }
- return;
- }
- int main (int argc, const char * argv [])
- {
- static const char * optv [] =
- {
- "qv",
- "file [ file ] [ ...] > file",
- "Atheros Clause 45 MDIO Instruction Block Assembler",
- "q\tquiet mode",
- "v\tverbose mode",
- (const char *) (0)
- };
- flag_t flags = (flag_t)(0);
- optind = 1;
- while ((c = getoptv (argc, argv, optv)) != -1)
- {
- switch (c)
- {
- case 'q':
- _setbits (flags, MDIO_SILENCE);
- break;
- case 'v':
- _setbits (flags, MDIO_VERBOSE);
- break;
- default:
- break;
- }
- }
- argc -= optind;
- argv += optind;
- if (isatty (STDOUT_FILENO))
- {
- error (1, ECANCELED, "stdout must be a file or pipe");
- }
- #if defined (WIN32)
- setmode (STDOUT_FILENO, O_BINARY);
- #endif
- instr = MDIO16_START (1, 0, count);
- write (STDOUT_FILENO, &instr, sizeof (instr));
- if (!argc)
- {
- assemble (flags);
- }
- while ((argc) && (* argv))
- {
- if (!freopen (* argv, "rb", stdin))
- {
- error (1, errno, "%s", * argv);
- }
- assemble (flags);
- argc--;
- argv++;
- }
- instr = MDIO16_START (1, 0, count);
- addr = count * sizeof (instr) + sizeof (instr);
- if ((addr % sizeof (uint32_t)))
- {
- uint32_t pad = 0;
- write (STDOUT_FILENO, &pad, sizeof (pad) - addr % sizeof (pad));
- }
- if (!lseek (STDOUT_FILENO, 0, SEEK_SET))
- {
- write (STDOUT_FILENO, &instr, sizeof (instr));
- }
- if (_anyset (flags, MDIO_VERBOSE))
- {
- fprintf (stderr, "%d instructions\n", count);
- }
- return (0);
- }
|