\
\    Mecrisp-Across - A Forth cross compiler for small MSP430 microcontrollers
\    Copyright (C) 2016  Matthias Koch
\
\    This program is free software: you can redistribute it and/or modify
\    it under the terms of the GNU General Public License as published by
\    the Free Software Foundation, either version 3 of the License, or
\    (at your option) any later version.
\
\    This program is distributed in the hope that it will be useful,
\    but WITHOUT ANY WARRANTY; without even the implied warranty of
\    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
\    GNU General Public License for more details.
\
\    You should have received a copy of the GNU General Public License
\    along with this program.  If not, see <http://www.gnu.org/licenses/>.
\


\ -----------------------------------------------------------------------------
\   Emit into a file
\ -----------------------------------------------------------------------------

\ write file via syscall interface                      uh 2015-09-26

\ : syscall ( r0 r1 r2 r3 r4 r5 r6 Syscall# -- r0 )

\ int creat(const char *pathname, mode_t mode);
8 constant #creat

8 base ! 
00400 constant S_IRUSR
00200 constant S_IWUSR
00100 constant S_IXUSR
decimal

: 0terminate ( addr len -- ) + 0 swap c! ;

: syscall-creat ( addr len mode -- fd )
  >r  dup >r here swap move  here r> 0terminate 
  here r> 0 0 0 0 0 #creat syscall ;


\ ssize_t write(int fd, const void *buf, size_t count);
4 constant #write
: syscall-write ( adr len fd -- len' )  -rot 0 0 0 0 #write syscall ; 

\ int close(int fd)
6 constant #close
: syscall-close ( fd -- ior ) 0 0 0 0 0 0 #close syscall ;

\ example: write logfile

0 variable logfile
: open-log ( addr len -- )  
   S_IRUSR S_IWUSR or syscall-creat  logfile ! ;

: write-log ( addr len -- )
   logfile @ syscall-write drop ; 

create newline 10 c,

: writeln-log ( addr len )
   write-log
   newline 1 write-log ;

: close-log ( -- )  logfile @ syscall-close drop ;

: log-emit ( c -- )
  sp@ 1 write-log drop
;  

\ -----------------------------------------------------------------------------
\   Write hexdump into a file
\ -----------------------------------------------------------------------------

: hexdump-file ( -- ) \ hexdump-file Name
  token open-log
  ['] log-emit hook-emit !
  hexdump
  ['] serial-emit hook-emit !
  close-log
;

\ -----------------------------------------------------------------------------
\   Write a disassembler listing into a file
\ -----------------------------------------------------------------------------

: disimage-file ( -- ) \ disasm-file Name
  token open-log
  ['] log-emit hook-emit !
  disimage
  ['] serial-emit hook-emit !
  close-log
;

\ -----------------------------------------------------------------------------
\   Write a crosscompiler listing into a file
\ -----------------------------------------------------------------------------

: t-listing-file ( -- ) \ t-listing-file Name
  token open-log
  ['] log-emit hook-emit !
  t-listing
  ['] serial-emit hook-emit !
  close-log
;

