File:  [gforth] / gforth / fi2c.fs
Revision 1.3: download - view: text, annotated - select for diffs
Fri Dec 11 22:54:27 1998 UTC (20 years, 5 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added further options to shrink a kernel down
Cleaned up conditional primitives (works now for C-generated part, too)
Cleaned up mach files for embedded architectures
Cleaned up options in the kernel

    1: \ Convert image to C include file
    2: 
    3: \ Copyright (C) 1998 Free Software Foundation, Inc.
    4: 
    5: \ This file is part of Gforth.
    6: 
    7: \ Gforth is free software; you can redistribute it and/or
    8: \ modify it under the terms of the GNU General Public License
    9: \ as published by the Free Software Foundation; either version 2
   10: \ of the License, or (at your option) any later version.
   11: 
   12: \ This program is distributed in the hope that it will be useful,
   13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15: \ GNU General Public License for more details.
   16: 
   17: \ You should have received a copy of the GNU General Public License
   18: \ along with this program; if not, write to the Free Software
   19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   20: 
   21: 0 Value image
   22: 0 Value bitmap
   23: 
   24: Create magicbuf 8 allot
   25: 
   26: Variable bswap?
   27: Variable tchars
   28: 
   29: : bswap ( n -- n' )  bswap? @ 0= ?EXIT  0
   30:     over 24 rshift $FF       and or
   31:     over  8 rshift $FF00     and or
   32:     over  8 lshift $FF0000   and or
   33:     over 24 lshift $FF000000 and or nip ;
   34: 
   35: : search-magic ( fd -- )  >r
   36:     BEGIN  magicbuf 8 r@ read-file throw  8 =  WHILE
   37: 	magicbuf s" Gforth" tuck compare 0=  UNTIL
   38:     ELSE  true abort" No magic found"  THEN
   39:     magicbuf 6 + c@ digit? drop tchars !
   40:     magicbuf 7 + c@ 1 and 0= [ pad off 1 pad ! pad c@ 1 = ] Literal = bswap? !
   41:     rdrop ;
   42: 
   43: Create image-header  4 cells allot
   44: Variable image-cells
   45: Variable bitmap-chars
   46: 
   47: : read-header ( fd -- )
   48:     image-header 4 cells rot read-file throw drop
   49:     image-header 2 cells + @ bswap tchars @ *
   50:     dup cell / image-cells ! 1- 8 cells / 1+ bitmap-chars !
   51:     image-cells @ cells allocate throw to image
   52:     bitmap-chars @ allocate throw to bitmap ;
   53: 
   54: : read-dictionary ( fd -- )  >r
   55:     image image-cells @ cells r> read-file throw drop ;
   56: 
   57: : read-bitmap ( fd -- )  >r
   58:     bitmap bitmap-chars @ r> read-file throw drop ;
   59: 
   60: : .08x ( n -- ) 0 <# # # # # # # # # 'x hold '0 hold #> type ;
   61: : .02x ( n -- ) 0 <# # # 'x hold '0 hold #> type ;
   62: 
   63: : .image ( -- )
   64:     image-cells @ 0 ?DO
   65: 	I 4 + I' min I ?DO  space image I cells + @ bswap .08x ." ," LOOP cr
   66: 	4 +LOOP ;
   67: 
   68: : .reloc ( -- )
   69:     bitmap-chars @ 0 ?DO
   70: 	I 8 + I' min I ?DO  space bitmap I + c@ .02x ." ," LOOP cr
   71: 	8 +LOOP ;
   72: 
   73: : read-image ( addr u -- )
   74:     r/o bin open-file throw >r
   75:     r@ search-magic
   76:     r@ file-position throw r@ read-header r@ reposition-file throw
   77:     r@ read-dictionary r@ read-bitmap r> close-file throw ;
   78: 
   79: : .imagesize ( -- )
   80:     image-header 3 cells + @ bswap 1 cells / tchars @ * .08x ;
   81: 
   82: : .relocsize ( -- )
   83:     bitmap-chars @ .08x ;
   84: 
   85: : fi2c ( addr u -- )  base @ >r hex
   86:     read-image
   87:     ." #include " '" emit ." forth.h" '" emit cr
   88:     ." Cell image[" .imagesize ." ] = {" cr .image ." };" cr
   89:     ." const char reloc_bits[" .relocsize ." ] = {" cr .reloc ." };" cr
   90:     r> base ! ;
   91: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>