TweetFollow Us on Twitter

XModem Transfer
Volume Number:2
Issue Number:12
Column Tag:Forth Forum

Batch Text File Transfer by XMODEM

By Jörg Langowski, MacTutor Editorial Board, Grenoble, France

If you don't happen to have a tape backup unit for your Macintosh, but access to some larger computer installation over a terminal line (with, usually, lots of disk space), there is a slow but secure way to keep backups of your files by uploading them to that machine. There are all sorts of communication utilities that help in doing so, most of them using the XMODEM protocol for up- and downloading files.

The telecommunications programs I am aware of at this moment are excellent tools for transferring single files to and from the Macintosh. If one wants to back up one full disk by such a file transfer, however, the task becomes a little tedious since one has to select one file at a time; particularly time-consuming if your disk has 327 short text files on it. The XMODEM standard does offer a batch transfer mode in which the filename is transferred followed by the file, and if anyone out there knows of a terminal program that has this mode implemented, please let me know.

For the time being, the transfer of one whole volume on a HFS disk from the Mac to another system by XMODEM batch transfer is a neat little project to implement in Forth. Besides being useful, it'll help us gain some insight into HFS file handling.

We will limit ourselves to transferring text files here. Since you all are aware that files on the Macintosh consist of the data and the resource fork, applications and other resource-containing files have to be changed into text files using a utility like BINHEX first. However based on our example, it is rather easy to implement the MacBinary format [a special standard combining both forks together with the Finder information into one structure: see MacTutor V2#1], and transfer files of any type automatically. At the present time, a text file backup system is fine for me, since the documents I most often have to back up, like manuscripts or current versions of Mach 2 and NEON source files, are text files anyway.

The XMODEM Batch Transfer Protocol

How does an XMODEM file transfer proceed? Each file is split up into sectors of 128 bytes each (a relic of old CP/M times), each sector starting at one. A simple file transfer (not batch mode) has the following protocol:

Simple File Transfer Protocol

Sender Receiver

waits for NAK,

80 sec timeout sends NAK at

10 sec intervals

Loop n times for n sectors

send header:

SOH ($01)

sector no. (0-255)

sector no. complement (255-0)

send sector data, 128 bytes

send checksum:

(header bytes + sector data ) mod 255

compute checksum and

compare with checksum


send ACK ($06) if OK and

NAK if not

If ACK received, increment

sector no.

otherwise resend sector

End Loop

send EOT ($04)

send ACK

The transfer may be cancelled at any time by the sender or receiver transmitting a Ctrl-X ($24) to the other end.

In batch mode, there is an additional protocol defined to transmit the filename before the file transfer starts:

Batch Mode Protocol

Sender Receiver

waits for NAK,

80 sec timeout sends NAK at

10 sec intervals

sends ACK

awaits filename characters,

1 sec timeout

on error -> above NAK

Loop 11 times

send filename char

(bit 7=0, upper case)

add to checksum receive filename char

await ACK 1 sec timeout add to checksum


End Loop

send Ctrl-Z

add to checksum receive Ctrl-Z

add to checksum

send checksum

receive checksum +


ACK if OK and "u" if not await ACK

- Normal file transfer starts -








expect NAK to start new

filename NAK

ACK for new file name

or EOT if finished

The routines necessary to implement this protocol are printed in Listing 1. [For the single file transfer protocol, I adapted some routines from an article by Robert Taylor in Dr. Dobbs Journal 83 (9/1983) p.66.]

The program in Listing 1 is best used by having some terminal emulation like Mockterminal running on the Macintosh at the same time. Mach2 users may append this code directly to the "Terminal Emulator" example on the Mach2 demo disk and then start the terminal emulator before doing the transfer. For other Forths, the program should be easily adaptable, you'll have to change the routines for setting the baud rate and for doing the modem input and output.

Look at the definition of the words send and send-filename and the inner loop of send for the implementation of the XMODEM protocol in Forth.

In order to be able to transfer one whole volume automatically, we must have some means for accessing the files on a volume one by one. The example contains some file and directory handling words for this purpose. I have also left in there some words I needed in testing (like $openWD) which are not used in the actual implementation, but could be useful at times.

The Mach2 word $open opens a file on the default volume, given a name string, and returns a file ID number. In order to access files in any arbitrary folder (= HFS volume), we have to get a volume ID number first and set the default volume to this ID. The word promptvolID calls the standard file package to prompt the user to select a file in the folder that is to be transferred. Then the volume ID of this folder is returned. Calling setvol with a volume ID sets the default volume. getidxfile takes as its input a volume ID and an index n and will look for the n-th file in the volume. The filename is returned in a global parameter block, parblock, and can be used to open the file with $open after setting the correct default volume. send-batch simply scans the default volume for all files and transfers them one by one. So far, for text files only. Have fun implementing the MacBinary standard.

Reader Feedback

I got several comments on the floating point routines that we published lately. Ed Moskowitz from Winfield, IL points out correctly that the routines will not work with the new Mach2 since they use D5 and D6. This is correct, and D5 and D6 have to be added to the MOVEM.L save and restore list at the routine entry and exits to fix this bug.

Mike Morton from Cambridge, MA had some useful comments to make about improving the speed of those routines. Excerpts from his letter:

"• If you're doing lots of _Pack4 calls, you can do a GetTrapAddress and call the package directly, saving 30 to 50 microseconds ( )

• Instead of BTST #7, D0, you can do TST.B D0 and use BMI to see if the bit is on.


• when you AND #$7FFFFF to both D3 and D1 in the division routine, it's faster to move the mask to a register just once and use it twice. ( )

• When you're doing multiplications [and divisions, JL], you might optimize for cases where some of the number is zero. So your four MULUs might test whether the operands are zero and handle that case explicitly. I spent some time writing 32-bit fixed-point multiply routines a while ago, and this helped a lot because many numbers don't have any bits on the bottom of the longword. If you expect many of the f.p. numbers to not have many significant bits, this helps some.

( ) "

Thanks, Ed and Mike, for your comments.


One letter came clear from Tasmania, from Phil Barnard:

" I am suffering from the new ROM syndrome at the moment, although the faster and more spacious drives are very pleasant after the original units. Having to replace the interrupt vectors for the interrupt button was a nuisance. As you are no doubt aware the new ROM/System does not present the familiar bomb-box, but an 'empty' dialog.

It may be that the new dialog can be used to resume in the same fashion as the old one, but I don't yet know how. G <return> sends it back into any loop that you may wish to exit. Looking at the register contents by typing A0 through D7 is of limited debugging use. Can it do anything else?"

[Why, I even installed a control panel with flashing LEDs and toggle switches on the top of my Mac. Why do you want anything else? ... Well, G 40F6D8, of course, you probably know by now that that is the address of _Exittoshell and will (sometimes) send you straight back to the Finder. I don't use it for many other things. JL]

"The following routine brings back the old familiar bomb-box on interrupt."

\ installation of custom interrupt vectors; Phil Barnard
\ prints out the seven vectors from the interrupt table
: get.ints ( -- ) cr
 $ 64 -base @ . cr \ VIA
 $ 68 -base @ . cr \ SCC
 $ 6C -base @ . cr \ VIA & SCC
 $ 70 -base @ . cr \ Debugging Button
 $ 74 -base @ . cr \     "
 $ 78 -base @ . cr \     "
 $ 7C -base @ . cr \     "


\ installs RTS in the last four (debugging) vectors in the interrupt 

: put.ints ( -- )
 $ 80 $ 70 do 'c RTS +base i -base ! 4 +loop get.ints ;

Thanks, Phil. I'll deal with more NEON stuff in the next column where I'll also have some time to write about the new release of NEON, version 2.0. Here only a few headlines:

• NEON 2.0 is fully HFS and Switcher compatible; a new class pathlist has been added to specify HFS search paths. The assembler supports the new ROM calls.

• Applications install as a one piece file, no need to keep a copy of the installed NEON kernel on the disk.

• A decompiler has been added that will handle simple definitions, classes, objects and methods.

• A number of bugs, including the famous IC! have been fixed.

More details about NEON 2.0 and other NEON things later. Till then, good holidays.

Listing 1: XMODEM batch file downloader

( Implementation of Modem7 file transfer protocol, 
 © 9/86 J. Langowski  for MacTutor )

only forth also mac also i/o also assembler

60 user fileID

10 constant maxerr   0 constant nul24 constant can
4 constant eot   6 constant ack  21 constant nak
1 constant soh   26 constant ctrl-z
117 constant bdnmch

" Select file on volume to upload:" constant seldirtext

variable time-ct variable nak-ct variable cksum    
variable filename 128 vallot
variable xbuffer 128 vallot
variable replyrecord 74 vallot

header parblock 12 allot  ( 12 bytes junk )
header iocompl   4 allot  ( at PB + 12 )
header iores     8 allot  ( at PB + 16 )
header ioref#    4 allot  ( at PB + 24 )
header iomisc    4 allot  ( at PB + 28 )
100 allot ( some elbow room )

: long-timeout  1000 time-ct ! ; (  17 sec timeout )
: short-timeout 100  time-ct ! ; ( 1.7 sec timeout )

: baud-rate ( - )
 1CC0A COMM1 mode  ( 9600 BAUD Xon/Xoff )
 ABORT" Serial Driver Error" ;  (8 bit, No Par, 2Stop)
: modout comm1 output emit console output ;
: fin can modout ;  : fini fin close-file ;

: timed-read { | tstart -- char }
 call tickcount -> tstart
 comm1 input
 ?terminal if key console input exit then
 call tickcount tstart -  time-ct @ > 
 console input  -1

: ?contrlx ?terminal 
 if key 24 = 
 if fini 1 abort" Control-X abort" then

: wait-ack { | errs -- }
 0 -> errs 
 begin  errs maxerr >  nak-ct @ maxerr > or
 if fini 1 abort" Max error count exceeded." then
 timed-read case
 -1  of errs 1+ -> errs ." Timeout" cr endof
 can of 1 abort" Remote Cancel" endof
 ack of 0 nak-ct !  exit endof
 nak of 1 nak-ct +! exit endof
 ( disp-chr 0 )

: send-setup
 standard-getfile  0= 
 if fin 1 abort" Cancelled." then
 0 nak-ct !

: read-sect { sector# | sect-start -- }
 sector# 128 * -> sect-start
 128 0
 do i sect-start + virtual c@ i xbuffer + c! loop

: send-filename
 ack modout 0 cksum c!
 12 1 do filename i + 
 c@ 127 and dup 95 > if 32 - then
 dup modout dup emit
 cksum c@ + cksum c!
 loop cr
 ctrl-z modout 
 cksum c@ ctrl-z + cksum c!
: send-hdr { sector# | -- }
 soh modout
 sector# 1+ 255 and dup modout 255 xor modout
 ." Sending " sector# . cr

: send-sect ( send sector in xbuffer via XMODEM )
 0 ( sum )
 128 0 do xbuffer i + c@ dup modout + 255 and loop
 cksum c!
: send-cksum cksum c@ modout ;

: end-send
 begin cr ." Sending EOT -" eot modout wait-ack
 nak-ct @ 0= until
 ." ack'd"

: get-file-length ( -- sectors )
 fileID w@ ['] ioref# w!  0 ['] iocompl !
 ['] parblock call geteof . cr
 ['] iomisc @ 128 /mod swap 
 if 1+ then
 dup ." File has " . ."  sectors" cr 
: send ( current file via XMODEM, closes file on exit )
 get-file-length 0
 i read-sect ( into xbuffer )
 i send-hdr send-sect send-cksum
 wait-ack nak-ct @ 0=

: prelude
 short-timeout  begin timed-read -1 = until
 long-timeout  ." Awaiting initial NAK: "
 wait-ack cr   short-timeout

: send-one send-setup prelude send ;

: end-batch ack modout eot modout ;

( *** file and directory  handling routines *** )

: getVolID { index | pb -- VRefNum / errcode 0 }
 ( ID for volume no. index )
   ['] parblock -> pb
 0 ['] iocompl !
 filename pb 18 + !
 index pb 28 + w!
 pb call getvolinfo
 ?dup if 0 else pb 22 + w@ then

CODE SFGetFile  ( x y prompt numTypes typeList ReplyRecord )
 MOVE.L (A6)+,A1 ( reply record )
 MOVE.L (A6)+,A0 ( type list )
 MOVE.L (A6)+,D3 ( numTypes )
 MOVE.L (A6)+,D2 ( prompt )
 MOVE.L (A6)+,D1 ( y-coord )
 MOVE.L (A6)+,D0 ( x-coord )

 MOVE.W D0,-(A7)
 MOVE.W D1,-(A7) ( where )
 MOVE.L D2,-(A7) ( prompt )
 MOVE.L #0,-(A7) ( no filter proc )
 MOVE.W D3,-(A7) ( num Types )
 MOVE.L A0,-(A7) ( type list )
 MOVE.L #0,-(A7) ( no dialog hook )
 MOVE.L A1,-(A7) ( reply record )

 MOVE.W #2,-(A7)

: promptvolID  ( -- VRefNum flag )
 100 50 seldirtext -1 0 replyrecord 
 replyrecord 6 + w@ l_ext
 replyrecord w@ l_ext

 MOVE.L (A6)+,A1
 MOVE.L (A6)+,A0
 MOVE.L (A6)+,D3
 MOVE.L (A6)+,D2
 MOVE.L (A6)+,D1
 MOVE.L (A6)+,D0

 MOVE.W D0,-(A7)
 MOVE.W D1,-(A7)
 MOVE.L D2,-(A7)
 MOVE.L D3,-(A7)
 MOVE.L A0,-(A7)
 MOVE.L A1,-(A7)

 MOVE.W #1,-(A7)

: $openWD { name | pb -- VRefNum / errcode 0 }
   ['] parblock -> pb
 0 ['] iocompl !
 name pb 18 + !
 0 pb 28 + w!
 pb 1 call HFSDispatch
 ?dup if 0 else pb 22 + w@ then

: closeWD { volume | pb -- rescode }
   ['] parblock -> pb
 0 ['] iocompl !
 volume pb 22 + w!
 pb 2 call HFSDispatch

: setVol { volume | pb -- res code }
   ['] parblock -> pb
 0 ['] iocompl !
 0 pb 18 + !
 volume pb 22 + w!
 pb call setVol
: getidxfile { volume index | pb -- res code }
   ['] parblock -> pb
 0 ['] iocompl !
 filename pb 18 + !( ioNamePtr )
 volume pb 22 + w! ( ioVRefNum ) 
 0 pb 26 + w!  ( ioFVersNum )
 index pb 28 + w!( ioFDirIndex )
 pb call getfileinfo 
 dup 0= if ." Found: " filename count type cr then

: openidxfile { volume index | -- res code }
 volume index getidxfile
 ?dup 0= if filename $open then

: find-files { | volume index -- }
 if -> volume 1 -> index
 volume index getidxfile
 index 1+ -> index
 else ." cancelled" . cr 
( *** main definitions for batch file transfer *** )
: display-filename
 12 1 do filename i + c@ 127 and dup 95 > if 32 - then emit
 loop cr
: send-batch{ volume | index }
 1 -> index  0 nak-ct ! 
 begin cr
 filename 1+ 11 32 fill
 volume index getidxfile 0= 
 volume setvol drop 
 filename $open dup 0> 
 if   fileID w!
 prelude send-filename
 timed-read cksum c@ <> 
 ." Checksum Error in filename, restarting" cr
 bdnmch modout
 ack modout
 send ( closes file )
 else drop 
 index 1+ -> index
 wait-ack end-batch
: upload 
 if send-batch 
 else cr ." Cancelled" cr

Community Search:
MacTech Search:

Software Updates via MacUpdate

GraphicConverter 10.5.1 - $39.95
GraphicConverter is an all-purpose image-editing program that can import 200 different graphic-based formats, edit the image, and export it to any of 80 available file formats. The high-end editing... Read more
Delicious Library 3.7 - Import, browse a...
Delicious Library allows you to import, browse, and share all your books, movies, music, and video games with Delicious Library. Run your very own library from your home or office using our... Read more
Adobe Animate CC 2017 - Anima...
Animate CC 2018 is available as part of Adobe Creative Cloud for as little as $19.99/month (or $9.99/month if you're a previous Flash Professional customer). Animate CC 2018 (was Flash CC) lets you... Read more
Adobe After Effects CC 2018 15.0 - Creat...
After Effects CC 2018 is available as part of Adobe Creative Cloud for as little as $19.99/month (or $9.99/month if you're a previous After Effects customer). The new, more connected After Effects CC... Read more
Adobe Premiere Pro CC 2018 12.0.0 - Digi...
Premiere Pro CC 2018 is available as part of Adobe Creative Cloud for as little as $19.99/month (or $9.99/month if you're a previous Premiere Pro customer). Adobe Premiere Pro CC 2018 lets you edit... Read more
Alarm Clock Pro 10.3 - $19.95
Alarm Clock Pro isn't just an ordinary alarm clock. Use it to wake you up in the morning, send and compose e-mails, remind you of appointments, randomize the iTunes selection, control an internet... Read more
Adobe Lightroom 20170919-1412-ccb76bd] -...
Adobe Lightroom is available as part of Adobe Creative Cloud for as little as $9.99/month bundled with Photoshop CC as part of the photography package. Lightroom 6 is also available for purchase as a... Read more
Adobe Illustrator CC 2018 22.0.0 - Profe...
Illustrator CC 2018 is available as part of Adobe Creative Cloud for as little as $19.99/month (or $9.99/month if you're a previous Illustrator customer). Adobe Illustrator CC 2018 is the industry... Read more
Hopper Disassembler 4.3.0- - Binary disa...
Hopper Disassembler is a binary disassembler, decompiler, and debugger for 32- and 64-bit executables. It will let you disassemble any binary you want, and provide you all the information about its... Read more
Adobe InDesign CC 2018 - Prof...
InDesign CC 2018 is available as part of Adobe Creative Cloud for as little as $19.99/month (or $9.99/month if you're a previous InDesign customer). Adobe InDesign CC 2018 is part of Creative Cloud.... Read more

ICEY (Games)
ICEY 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: ICEY is a 2D side-scrolling action game. As you follow the narrator's omnipresent voice, you will see through ICEY's eyes and learn the... | Read more »
The best new games we played this week -...
We've made it, folks. Another weekend is upon us. It's time to sit back and relax with the best new releases of the week. Puzzles, strategy RPGs, and arcade games abound this week. There's a lot of quality stuff to unpack this week, so let's hop... | Read more »
Wheels of Aurelia (Games)
Wheels of Aurelia 1.0.1 Device: iOS Universal Category: Games Price: $3.99, Version: 1.0.1 (iTunes) Description: | Read more »
Halcyon 6: Starbase Commander guide - ti...
Halcyon 6 is a well-loved indie RPG with stellar tactical combat and some pretty good writing, too. It's now landed on the App Store, so mobile fans, if you're itching for a good intergalactic adventure, here's your game. Being a strategy RPG, the... | Read more »
Game of Thrones: Conquest guide - how to...
Fans of base building games might be excited to know that yet another entry in the genre has materialized - Game of Thrones: Conquest. Yes, you can now join the many kingdoms of the famed book series, or create your own, as you try to conquer... | Read more »
Halcyon 6: Starbase Commander (Games)
Halcyon 6: Starbase Commander Device: iOS Universal Category: Games Price: $6.99, Version: (iTunes) Description: An epic space strategy RPG with base building, deep tactical combat, crew management, alien diplomacy,... | Read more »
Legacy of Discord celebrates its 1 year...
It’s been a thrilling first year for fans of Legacy of Discord, the stunning PvP dungeon-crawling ARPG from YOOZOO Games, and now it’s time to celebrate the game’s first anniversary. The developers are amping up the festivities with some exciting... | Read more »
3 reasons to play Thunder Armada - the n...
The bygone days of the Battleship board game might have past, but naval combat simulators still find an audience on mobile. Thunder Armada is Chinese developer Chyogames latest entry into the genre, drawing inspiration from the explosive exchanges... | Read more »
Experience a full 3D fantasy MMORPG, as...
Those hoping to sink their teeth into a meaty hack and slash RPG that encourages you to fight with others might want to check out EZFun’s new Eternity Guardians. Available to download for iOS and Android, Eternity Guardians is an MMORPG that lets... | Read more »
Warhammer Quest 2 (Games)
Warhammer Quest 2 1.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0 (iTunes) Description: Dungeon adventures in the Warhammer World are back! | Read more »

Price Scanner via

12″ iPad Pros on sale for $50 off MSRP, no ta...
Adorama has 12″ iPad Pros on sale today for $50 off MSRP. Shipping is free, and Adorama charges sales tax in NY & NJ only: – 12″ 64GB iPad Pro: $749, save $50 – 12″ 256GB iPad Pro: $899, save $50... Read more
9″ iPads on sale for $30 off, starting at $29...
MacMall has 9″ iPads on sale for $30 off including free shipping: – 9″ 32GB iPad: $299 – 9″ 128GB iPad: $399 Read more
Apple restocks full line of refurbished 13″ M...
Apple has restocked a full line of Apple Certified Refurbished 2017 13″ MacBook Pros for $200-$300 off MSRP. A standard Apple one-year warranty is included with each MacBook, and shipping is free.... Read more
13″ 3.1GHz/256GB MacBook Pro on sale for $167...
Amazon has the 2017 13″ 3.1GHz/256GB Space Gray MacBook Pro on sale today for $121 off MSRP including free shipping: – 13″ 3.1GHz/256GB Space Gray MacBook Pro (MPXV2LL/A): $1678 $121 off MSRP Keep an... Read more
13″ MacBook Pros on sale for up to $120 off M...
B&H Photo has 2017 13″ MacBook Pros in stock today and on sale for up to $120 off MSRP, each including free shipping plus NY & NJ sales tax only: – 13-inch 2.3GHz/128GB Space Gray MacBook... Read more
15″ MacBook Pros on sale for up to $200 off M...
B&H Photo has 15″ MacBook Pros on sale for up to $200 off MSRP. Shipping is free, and B&H charges sales tax in NY & NJ only: – 15″ 2.8GHz MacBook Pro Space Gray (MPTR2LL/A): $2249, $150... Read more
Roundup of Apple Certified Refurbished iMacs,...
Apple has a full line of Certified Refurbished 2017 21″ and 27″ iMacs available starting at $1019 and ranging up to $350 off original MSRP. Apple’s one-year warranty is standard, and shipping is free... Read more
Sale! 27″ 3.8GHz 5K iMac for $2098, save $201...
Amazon has the 27″ 3.8GHz 5K iMac (MNED2LL/A) on sale today for $2098 including free shipping. Their price is $201 off MSRP, and it’s the lowest price available for this model (Apple’s $1949... Read more
Sale! 10″ Apple WiFi iPad Pros for up to $100...
B&H Photo has 10.5″ WiFi iPad Pros in stock today and on sale for $50-$100 off MSRP. Each iPad includes free shipping, and B&H charges sales tax in NY & NJ only: – 10.5″ 64GB iPad Pro: $... Read more
Apple iMacs on sale for up to $130 off MSRP w...
B&H Photo has 21-inch and 27-inch iMacs in stock and on sale for up to $130 off MSRP including free shipping. B&H charges sales tax in NY & NJ only: – 27″ 3.8GHz iMac (MNED2LL/A): $2179 $... Read more

Jobs Board

*Apple* Retail - Multiple Positions - Apple,...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
Commerce Engineer, *Apple* Media Products -...
Commerce Engineer, Apple Media Products (New York City) Job Number: 113028813New York City, New York, United StatesPosted: Sep. 20, 2017Weekly Hours: 40.00 Job Read more
US- *Apple* Store Leader Program - Apple (Un...
US- Apple Store Leader Program Job Number: VariousUnited StatesPosted: Oct. 19, 2017Retail Store Job Summary Learn and grow as you explore the art of leadership at Read more
Product Manager - *Apple* Pay on the *Appl...
Job Summary Apple is looking for a talented product manager to drive the expansion of Apple Pay on the Apple Online Store. This position includes a unique Read more
*Apple* Retail - Multiple Positions - Farmin...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.