home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / verilog / 318 < prev    next >
Encoding:
Text File  |  1992-08-20  |  7.7 KB  |  267 lines

  1. Path: sparky!uunet!cs.utexas.edu!swrinde!mips!mips!decwrl!sgi!rigden.wpd.sgi.com!rpw3
  2. From: rpw3@rigden.wpd.sgi.com (Rob Warnock)
  3. Newsgroups: comp.lang.verilog
  4. Subject: Re: Simulating processor architectures
  5. Message-ID: <or6oej4@sgi.sgi.com>
  6. Date: 21 Aug 92 04:22:52 GMT
  7. Sender: rpw3@rigden.wpd.sgi.com
  8. Organization: Silicon Graphics, Inc.  Mountain View, CA
  9. Lines: 256
  10.  
  11. kuttanna@eemips.tamu.edu (Belliappa Kuttanna) writes:
  12. +---------------
  13. |  the gnu CC compiler can generate code using this instruction set.
  14. |  However, I do not know how to generate the Verilog input from the
  15. |  a.out or the assembly code file, without having to develop some 
  16. |  software specifically for this purpose.
  17. +---------------
  18.  
  19. Actually, the easiest thing would be to "develop some software specifically
  20. for this purpose". That's what I did. I wrote a hack about a year ago I
  21. call "o2rmh", that takes a couple of formats of COFF files (AMD 29000
  22. standard and gas/29k) and writes files suitable for reading into Verilog
  23. with $readmemh(). You use it like this:
  24.  
  25.     % cat foo.s
  26.     ; test 29030 I-cache -- XXX stub only
  27.  
  28.         .include "std0.h"
  29.  
  30.         mtsrim CFG, 0           ; enable cache
  31.         mtsrim CPS, 0x73        ; leave FZ mode
  32.         nop                     ; don't jump back here (erratum #4)
  33.         nop
  34.  
  35.         nop            ; ditto
  36.         nop
  37.         nop
  38.         const   v0, 0x1234
  39.     loop:
  40.         jmp     loop            ; tight loop [note "store" in dly slot]
  41.          store  0, 0, v0, 0x98  ; i.e. loc 98 gets 1234
  42.         nop
  43.         nop
  44.  
  45.         nop                     ; filler for prefetch
  46.         nop
  47.         nop
  48.         nop
  49.  
  50.         .end
  51.  
  52.     % gas29 -o foo.o foo.s
  53.  
  54.     % o2rmh -o foo.rmh foo.o
  55.     'foo.o': GNU .o/a.out format
  56.  
  57.     % cat foo.rmh
  58.     // [generated by o2rmh]
  59.  
  60.     04000300 04000273 70400101 70400101 
  61.     70400101 70400101 70400101 03126034 
  62.     a0000000 1f006098 70400101 70400101 
  63.     70400101 70400101 70400101 70400101 
  64.  
  65. Then in your Verilog simulation you just $readmemh("foo.rmh", instruction_mem).
  66.  
  67. Attached below find the source for "o2rmh.c". It's an ugly hack, is missing
  68. many features (relocation, etc.), and may not do what you want, but what do
  69. you expect? I threw it together in an hour or two.
  70.  
  71. Note: I have *not* included the required AMD-specific include file "coff.h"
  72. (which probably contains licensed AT&T Unix material), so you will have to
  73. fix that part up for yourself. [Hint: Just rip out (or fix) the part that
  74. starts with "if (fh->f_magic == SIPFBOMAGIC)". The demo output above uses
  75. the GNU assembler format, which *is* included.
  76.  
  77. In any case, you should be able to modify it easily for your format...
  78.  
  79.  
  80. -Rob
  81.  
  82. -----
  83. Rob Warnock, MS-9U/510        rpw3@sgi.com
  84. Silicon Graphics, Inc.        (415)390-1673
  85. 2011 N. Shoreline Blvd.
  86. Mountain View, CA  94043
  87.  
  88.  
  89. ============== attachment ===============================================
  90. /*
  91. ** Copyright 1991, 1992 Silicon Graphics Inc.
  92. **
  93. ** Permission to use, copy, modify, and distribute this software and its
  94. ** documentation for any purpose and without fee is hereby granted, provided
  95. ** that the above copyright notice appears in all copies and that both that
  96. ** copyright notice and this permission notice appear in supporting
  97. ** documentation, and that the name of SGI not be used in advertising
  98. ** or publicity pertaining to distribution of the software without specific,
  99. ** written prior permission.  SGI makes no representations about the
  100. ** suitability of this software for any purpose.  It is provided "as is"
  101. ** without express or implied warranty.
  102. **
  103. ** SGI DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  104. ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SGI
  105. ** BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  106. ** WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  107. ** OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  108. ** CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  109. **
  110. **
  111. ** Author: Rob Warnock <rpw3@sgi.com>
  112. */
  113.  
  114. #include <stdio.h>
  115. #include <errno.h>
  116. #include <sys/types.h>
  117. #include <malloc.h>
  118. #include <string.h>
  119. #include "coff.h"        /* Note: 29000 version of COFF */
  120.  
  121. struct gnuhdr            /* simpler to copy than include */
  122. {
  123.   u_long a_info;        /* Use macros N_MAGIC, etc for access */
  124. #define GNUOBJMAGIC 0407
  125.   u_long a_text;        /* length of text, in bytes */
  126.   u_long a_data;        /* length of data, in bytes */
  127.   u_long a_bss;            /* length of bss, in bytes */
  128.   u_long a_syms;        /* length of sym tab data in file, in bytes */
  129.   u_long a_entry;        /* start address */
  130.   u_long a_trsize;        /* length of reloc info for text, in bytes */
  131.   u_long a_drsize;        /* length of reloc info for data, in bytes */
  132. };
  133.  
  134. extern char *optarg;
  135. extern int optind, opterr;
  136. char    *myname;
  137.  
  138. int    fd;
  139.  
  140. /*
  141.  * error message routine (non-varargs version):
  142.  * fatal(printf_args...) ==> fprintf(stderr,printf_args...), then exit(errno).
  143.  */
  144.  
  145. fatal(fmt, x0,x1,x2,x3,x4,x5,x6,x7,x8,x9)
  146. char * fmt;
  147. int x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;
  148. {
  149.     char real_fmt[1024];
  150.     extern int errno;
  151.  
  152.     sprintf(real_fmt, "%s: %s\n", myname, fmt);
  153.     fprintf(stderr, real_fmt, x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
  154.     exit(errno?errno:EINVAL);
  155. }
  156.  
  157. /*
  158.  * the usual main program template...
  159.  */
  160. main(int argc, char **argv)
  161. {
  162.     int c, i, n, errs = 0;
  163.  
  164.     if ((myname = strrchr(argv[0], '/') + 1) == ((char*)NULL) + 1)
  165.         myname = argv[0];
  166.  
  167.     while ((c = getopt(argc, argv, "o:")) != EOF) switch (c) {
  168.     case 'o':
  169.         if (freopen(optarg, "w", stdout) == NULL)
  170.             fatal("cannot open output file '%s'", optarg);
  171.         break;
  172.     case '?':
  173.     default:  ++errs;
  174.     }
  175.     if (argc - optind < 1 || errs) {
  176.         fprintf(stderr, "usage: o2rmh [-o outputfile] obj_file(s)...\n");
  177.         exit(1);
  178.     }
  179.  
  180.     while (argc - optind > 0)
  181.         read_and_load(argv[optind++]);
  182.     exit(0);
  183. }
  184.  
  185. /*
  186.  * This version of read_and_load() is about as subtle as a 20 kton nuke
  187.  */
  188. read_and_load(char *fname)
  189. {
  190. #define MONGO (262144 + 20000)        /* max SRAM + room for some syms */
  191.     int fd2 = open(fname, 0);
  192.     static void *fbuf;
  193.     int len;
  194.     struct filehdr    *fh;        /* AMD COFF header */
  195.     struct aouthdr    *ah;
  196.     struct scnhdr    *sh;
  197.     struct gnuhdr    *gh;        /* GNU a.out header */
  198.     u_char        *txtp;
  199.     int        tlen;
  200.  
  201.     if (fd2 < 0) {
  202.         fatal("Can't open file '%s': %s (%d)\n",
  203.             fname, sys_errlist[errno], errno);
  204.     }
  205.     if (!fbuf && !(fbuf = malloc(MONGO))) {
  206.         fatal("read_and_load: Can't get enough memory\n");
  207.     }
  208.     if ((len = read(fd2, fbuf, MONGO)) < 0) {
  209.         fatal("Error reading file '%s': %s (%d)\n",
  210.             fname, sys_errlist[errno], errno);
  211.     }
  212.     if (len == MONGO) {
  213.         fatal("File '%s' too large\n", fname);
  214.     }
  215.     /*
  216.      * alias to various possibilities of .o formats
  217.      */
  218.     fh = (struct filehdr *)fbuf;
  219.     gh = (struct gnuhdr *)fbuf;
  220.     if (fh->f_magic == SIPFBOMAGIC) {    /* first try AMD COFF format */
  221.         fprintf(stderr, "'%s': COFF .o/a.out format\n", fname);
  222.         sh = (struct scnhdr *)((u_long)fbuf + sizeof(*fh));
  223.         if (fh->f_opthdr) {
  224.         ah = (struct aouthdr *)sh;
  225.         sh = (struct scnhdr *)((u_long)sh + fh->f_opthdr);
  226.         } else
  227.         ah = 0;
  228.         if (!(sh->s_flags & STYP_TEXT)) {
  229.         fatal("File '%s' 1st section type (%x) not executable text\n",
  230.             fname, sh->s_flags);
  231.         }
  232.         txtp = (u_char *)fbuf + sh->s_scnptr;
  233.         tlen = sh->s_size;
  234.     } else if (gh->a_info == GNUOBJMAGIC) {    /* how about GNU .o? */
  235.         fprintf(stderr, "'%s': GNU .o/a.out format\n", fname);
  236.         txtp = (u_char *)fbuf + sizeof(*gh);
  237.         tlen = gh->a_text;
  238.     } else {
  239.         fatal("File '%s' bad magic number %x, no known Am29000 .o format\n",
  240.             fname, fh->f_magic);
  241.     }
  242.     if (tlen % sizeof(u_long)) {
  243.         fatal("File '%s' text section size (%d) not whole words\n",
  244.             fname, tlen);
  245.     }
  246.     do_dma(txtp, /*opt_LoadAddr,*/ tlen);
  247.     /* opt_LoadAddr += tlen; */        /* assert: tlen%4 == 0 */
  248. }
  249.  
  250. /*
  251.  * Output image in $readmemh() format.
  252.  * (Historical name, from when the object file
  253.  * would actually get downloaded at this point...)
  254.  */
  255. do_dma(u_char *p, int n)
  256. {
  257.     int i;
  258.  
  259.     printf("// [generated by o2rmh]\n\n");
  260.     for (i = 0, n/= 4; i < n; ++i) {
  261.         printf("%8.8x ", ((u_long *)p)[i]);
  262.         if (i%4 == 3)
  263.             printf("\n");
  264.     }
  265.     printf("\n");
  266. }
  267.