MGTize

Description of the page content

A wrapper that makes it easier to use pyz80 to create MGT disk images for SAM Coupé.

Tags:

MGTize is a wrapper for Andrew Collier's pyz80, a Z80 assembler focused on cross development for the SAM Coupé. The goal of MGTize is to make it easier to create MGT disk images with pyz80.

In order to create a MGT disk image with pyz80 you have to include every single file with the -I option, and also assemble any Z80 source file (it can be empty, but you have to assemble something). The command line can get quite long:

touch empty.z80s
pyz80 -I pict1.png -I pict2.pbm -I pict3.pgm -I pict4.ppm -I too.jpg -o result.mgt.gz empty.z80s
rm empty.z80s

MGTize makes it easier: Its first parameter is the name of the new MGT disk image (the ".mgt.gz" will be added or completed if needed; the file will be overwritten). All other parameters are names of files to be included — and shell patterns can be used. Of course, there's no need to indicate a Z80 source file: MGTize creates an empty temporary file to "assemble" and removes it at the end. For example, the equivalent of the previous command would be the following:

mgtize result pict?.p* too.jpg

MGTize is written in Forth with Gforth. It requires two Forth libraries: Forth Foundation Library and Galope (not published yet, but the required definitions are pasted in the source).

Simple installation instructions are included in the source code.

Source code

#! /usr/bin/env gforth

\ mgtize.fs
\ MGTize (version B-00-20150809)
\ This program creates SAMDOS MGT disk images.

\ Copyright (C) 2012 Marcos Cruz (programandala.net)

\ MGTize 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 2 of the License, or
\ (at your option) any later version. See:
\
\ MGTize 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/.

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Description

\ MGTize creates a gzip-compressed SAMDOS MGT disk image with the
\ desired files.  The MGT disk image can be used with SimCoupe, the
\ SAM Coupé emulator (<http://simcoupe.org>), or copied to a diskette.
\
\ MGTize is a wrapper for Andrew Collier's pyz80 (version 1.1,
\ 2007-04-13): <http://www.intensity.org.uk/samcoupe/pyz80.html>.
\
\ MGTize was written to make it easier to use pyz80 when you just want
\ to create a MGT disk image with several files, not to compile any
\ Z80 code. 
\
\ MGTize is written in Forth with Gforth:
\ <http://gnu.org/software/gforth>,
\ <http://www.complang.tuwien.ac.at/forth/gforth/>.

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Installation on Linux systems

\ 1. Gforth (http://gnu.org/software/gforth) and the Forth Foundation
\ Library (http://irdvo.github.io/ffl/) must be installed.
\
\ 2. Make sure this source file is executable, with the following
\ command:
\
\   chmod ugo+x mgtize.fs
\
\ 3. Copy, move or link this source file to your path (ussually to
\ </usr/local/bin/>), with the desired name (e.g. without the
\ extension). Example command:
\
\   sudo ln mgtize.fs /usr/local/bin/mgtize

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ History

\ 2012-12-09: Version A-00-20121209.
\ 2012-12-10: Version A-01-20121210. Improved: the file extension
\ is added or completed when needed; better arguments check;
\ clearer usage instructions.
\ 2013-07-07: Typo fixed.
\ 2013-07-28: Change in '+extension'.
\ 2014-05-29: Changed the stack notation of strings. New: 'fpath'.
\ 2015-01-10: Changes in comments and header.
\ 2015-08-07: The Galope definitions are pasted insted of included,
\ until the library is published. Version B-00.
\ 2015-08-09: Installation instructions.

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Requirements

\ fpath path+ ~/forth  \ XXX OLD TMP -- until the next version of Gforth supports <~/.gforthrc>

\ From Galope
\ (<http://programandala.net/en.program.galope.html>)

\ require galope/ends-question.fs  \ 'ends?'
\ require galope/sconstant.fs  \ 'sconstant'
\ require galope/svariable.fs  \ 'svariable'

\ XXX TMP -- pasted:

: fourth  ( x1 x2 x3 x4 -- x1 x2 x3 x4 x1 )
  \ Copy fourth element on the stack onto top of stack.
  3 pick
  ;
: ends? ( ca1 len1 ca2 len2 -- ca1 len1 wf )
  \ Check end of string:
  \ Is ca2 len2 the end of ca1 len1?
  \ ca1 len1 = long string
  \ ca2 len2 = end to check
  2over  dup fourth - /string  compare 0=
  ;
: sconstant  (  a len "name" -- )
  create  s,
  does>  ( -- a len ) ( pfa ) count
  ;
s" /COUNTED-STRING" environment? 0= [if]  255  [then]
constant /counted-string
: svariable  ( "name" -- )
  \ Create a string variable.
  create 0 c, /counted-string allot align
  ;

\ From Forth Foundation Library
\ (<http://code.google.com/p/ffl/>)
require ffl/str.fs  \ dynamic strings

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ System

\ The Forth Foundation Library's config.fs file nullifies the
\ words 'argc' and 'arg', and defines its own versions '#args'
\ and 'arg@'.

[defined] ffl.version [if]

  \ Re-create the original Gforth words 'argc' and 'arg', based
  \ on the words defined by the Forth Foundation Library's
  \ config.fs. 

  warnings @ warnings off

  variable argc  #args 1+ argc !
  : arg  ( n -- a u )
    1- arg@
    ;

  warnings !

[then]

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Variables and constants

svariable mgt-file
svariable input-file
s" mgtize.fs" sconstant command  \ name of this file

\ Temporary empty file used as fake assembler file for pyz80:
s" .tmp-" command s+  s" -.z80s" s+ sconstant z80-file

variable z80-file-id

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Strings

: s&  ( ca1 len1 ca2 len2 -- ca3 len3 )
  \ Concatenate two strings adding a space between them.
  2>r s"  " s+ 2r> s+
  ;
: str-space-escape  ( str -- )
  \ Replace all spaces of a dynamic string with escaped spaces.
  \ str = dynamic string address
  >r s" \ " s"  " r> str-replace
  ;
str-create tmp-str
: escaped  ( ca1 len1 -- ca2 len2 )
  \ Escape all the spaces of a string.
  tmp-str str-set
  tmp-str str-space-escape
  tmp-str str-get
  ;

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ pyz80 and its parameters

: -i-parameters  ( -- a u )
  \ Return a string with all '-I' parameters required by pyz80.
  \ ca1 len1 = files to be imported, with their '-I' prefixes
  s" "
  argc @ 2 ?do
    s"  -I " i arg escaped s+ s+
  loop
  ;
: create-z80-file  ( -- )
  \ Create the temporary fake z80 file.
  z80-file r/o create-file abort" Error creating the fake z80 file."
  z80-file-id !
  ;
: delete-z80-file  ( -- )
  \ Delete the temporary fake z80 file.
  z80-file delete-file abort" Error deleting the fake z80 file."
  ;
: +extension  ( ca1 len1 -- ca2 len2 )
  \ Add the file extension to the output file, if needed.
  s" .mgt.gz" ends? 0= if
    \ xxx first version
    \ s" .mgt" ends? if  s" .gz"  else  s" .mgt.gz"  then  s+
    \ xxx 2013-07-28 new version
    s" .mgt" ends? 0= if  s" .mgt" s+  then  s" .gz" s+
  then
  ;
: -o-parameter  ( -- a u )
  \ Return a string with the '-o' parameter required by pyz80.
  s" -o "
  1 arg  \ parameter 1 is the name of the MGT disk image
  +extension
  2dup mgt-file place  \ keep a copy of the file name for later use
  s+
  ;
: pyz80  ( ca1 len1 ca2 len2 -- )
  \ Execute pyz80 with the provided parameters.
  \ ca1 len1 = files to be imported, with their '-I' prefixes
  \ ca2 len2 = name of the MGT disk image
  create-z80-file
  s" pyz80 " 2swap s+ 2swap s& z80-file s& system
  delete-z80-file
  ;
: mgt ( -- )
  \ Convert the parameters and execute pyz80 with them.
  -i-parameters -o-parameter pyz80
  ;

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Usage

: .command  ( -- )
  \ Print the name of this file.
  command type
  ;
: cri  ( -- )
  \ Start a new line with an indentation.
  cr 4 spaces
  ;
: usage  ( -- )
  \ Show the usage instructions.
  cr ." Usage:" cr
  cr ." The first parameter is the MGT disk image file name."
  cr ." Its extension '.mgt.gz' will be automatically added or completed if needed."
  cr ." WARNING: If the file already exists, it will be overwritten." cr
  cr ." The second parameter is a space separated list of files to be added to the disk image."
  cr ." Shell patterns can be used." cr
  cr ." Examples:" cr
  cri .command ."  mynewdiskimage myfile.bin" cr
  cri .command ."  mynewdiskimage myfile1.bin myfile2.txt" cr
  cri .command ."  mynewdiskimage img*.png data??.*" cr
  ;

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Debug

: .args  ( -- )
  \ Show all parameters.
  argc @ 0 ?do
    i dup . arg type cr
  loop
  ;

\ ." echo $0 --" s" echo $0" system ." --" cr cr  \ xxx debug check
\ 0 arg type cr  \ xxx debug check

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Main

: check  ( -- )
  \ Make sure the number of parameters is 3 or more.
  argc @ 3 < if  usage bye  then
  ;
: main  ( -- )
  check mgt
  ;
main  bye

Downloads

Related pages

mkmgt
A MGT disk image creator for ZX Spectrum's GDOS, G+DOS and Beta DOS.