Printing from Atari BASIC: LPRINT, P:, Control Codes and Screen Dumps
On an Atari, the printer is a device called P:, reached through the same input/output system as the screen and the disk. That makes simple printing very easy. It also explains most of the oddities people hit: lines that start in column 41, printers that never print, and graphics with holes in them. This guide covers the BASIC side from the first LPRINT to a dot-graphics screen dump, and ends with ways to print today without a 1980s printer.
What you'll learn: LPRINT and when not to use it, OPEN/PRINT #/CLOSE, what the OS printer handler does with your text, what the 850 interface changes, formatting a neat report, sending escape codes, printing dot graphics, and sending "printer" output to a file or a modern PC.
What you need: Atari BASIC and either a printer (an Atari SIO printer, or a parallel printer through an 850 or similar interface) or an emulated one. Every example can also write to a disk file, so you can try them with no printer at all.
1. The quick ways
LPRINT "HELLO, PRINTER"
LIST "P:"
LIST "P:",100,200
LPRINT (abbreviated LP.) prints one line. LIST "P:" prints your program listing; add line numbers to print only part of it. These work with any printer the OS can talk to.
LPRINT is a shortcut: it opens channel 7 to P:, prints and closes the channel again. That causes the most common printing bug. With a semicolon or comma at the end, LPRINT A$; closes the channel without finishing the line. The handler pads what it has with blanks to 40 characters and sends it, so the next LPRINT starts in column 41. Through an 850 interface it is worse: no end-of-line is sent, and most printers then don't print the line at all. So never end an LPRINT with ; or ,.
2. OPEN, PRINT # and CLOSE
For anything more than single lines, open a channel yourself and keep it open:
10 OPEN #2,8,0,"P:"
20 PRINT #2;"TESTING";
30 PRINT #2;"123"
40 CLOSE #2
This prints TESTING123 on one line, because the trailing semicolon in line 20 now does what you expect. Use channels 1–5. BASIC reserves 0 (the screen editor), 6 (graphics screens) and 7 (used by LPRINT and by LIST, LOAD and RUN with a device name). ? is short for PRINT, so ?#2;"X" works too. Always CLOSE: it sends the last partial line.
3. What the P: handler does with your text
The OS printer handler doesn't send characters one at a time. It collects them in a 40-byte buffer at locations 960–999 ($3C0–$3E7):
- When 40 characters have arrived, it sends the block to the printer and starts a new one. A long line simply goes out as several blocks.
- When an end-of-line character (ATASCII 155) arrives, it fills the rest of the block with blanks and sends it at once.
CLOSEsends any partial block, padded with blanks.
The block size is set by the second auxiliary byte of the OPEN. Normal mode (the default) uses 40-byte blocks. OPEN #2,8,68,"P:" asks for double-width mode with 20-byte blocks, and 83 asks for sideways printing with 29-byte blocks, which only the Atari 820 printer supports.
4. Printers on an 850 interface
Most Atari owners used a standard parallel printer through the Atari 850 Interface Module, which answers as the printer and passes each 40-byte block to its PARALLEL INTERFACE port. Its technical manual spells out the rules that matter:
- End-of-line becomes a carriage return (13). No line feed is added, so set the printer to add a line feed after each carriage return (usually a DIP switch), or every line prints on top of the last.
- Nothing else is translated. Bit 7 passes through unchanged, so escape codes and graphics bytes reach the printer intact.
- Everything after an EOL in a block is ignored. That is how the handler's blank padding stays off the paper.
- Too many carriage movements in one block (backspaces, forward or reverse line feeds) can make the printer too slow to accept the next block, giving ERROR 139. The cure is to keep to one carriage movement per 40-byte block, padding with
CHR$(0)if necessary.
The printer must accept each 40-byte block within 30 seconds, or four seconds on early 850s. Otherwise the result is an error, usually 139. After a failure, STATUS #2,S followed by PEEK(746) shows the interface's status byte: 128 means the last operation succeeded and 132 means the printer stayed BUSY too long.
5. A neat report
Printers are fixed-pitch, so columns line up if you pad every field to a fixed width. This program prints a small priced list with right-aligned numbers and two decimal places. Atari BASIC has no PRINT USING, so lines 500–520 build the money string by hand.
10 DIM D$(20),N$(14),B$(25),V$(10)
20 D$="P:"
30 B$=" ":B$(25)=" ":B$(2)=B$
40 OPEN #1,8,0,D$
50 PRINT #1;"ITEM QTY PRICE"
60 PRINT #1;"---------------------------"
70 T=0:FOR I=1 TO 4:READ N$,Q,P
80 PRINT #1;N$;B$(1,14-LEN(N$));
90 V$=STR$(Q):PRINT #1;B$(1,3-LEN(V$));V$;
100 GOSUB 500:PRINT #1;B$(1,10-LEN(V$));V$
110 T=T+Q*P:NEXT I
120 PRINT #1;"---------------------------"
130 P=T:GOSUB 500:PRINT #1;"TOTAL";B$(1,22-LEN(V$));V$
140 CLOSE #1:PRINT "REPORT SENT TO ";D$:END
200 DATA DISK BOX,2,12.5,JOYSTICK,1,9.99
210 DATA RIBBON,12,3.25,CABLE,3,7
500 C=INT(P*100+0.5):W=INT(C/100):C=C-W*100
510 V$=STR$(W):V$(LEN(V$)+1)=".":IF C<10 THEN V$(LEN(V$)+1)="0"
520 V$(LEN(V$)+1)=STR$(C):RETURN
The output is:
ITEM QTY PRICE
---------------------------
DISK BOX 2 12.50
JOYSTICK 1 9.99
RIBBON 12 3.25
CABLE 3 7.00
---------------------------
TOTAL 94.99
Line 20 is the only thing that names the printer. Change it to D$="D:REPORT.TXT" and the same report goes to a disk file. Make sure each field width is larger than anything that can go in it: B$(1,0) is ERROR 5. Commas in a PRINT list jump to tab stops every 10 columns (location 201 sets the spacing). That works, but explicit padding is easier to control.
6. Control codes
Printers take commands as escape sequences: ESC (ATASCII and ASCII 27) followed by one or more characters, sent with CHR$() inside a PRINT. These are the codes of Atari's XMM801 dot-matrix printer. Its graphics mode is Epson-compatible and its codes follow the Epson pattern; ESC E and ESC F, for instance, switch bold on and off on Epson printers too. Check your own printer's manual before relying on any of them.
| Effect | Codes (decimal) |
|---|---|
| Bold on / off | 27,69 (ESC E) / 27,70 (ESC F) |
| Double-strike on / off | 27,71 (ESC G) / 27,72 (ESC H) |
| Double width on | 27,87,1 (ESC W 1) |
| Elite (12 characters per inch) | 27,77 (ESC M) |
| Six / eight lines per inch | 27,54 (ESC 6) / 27,56 (ESC 8) |
| Line spacing of n/72 inch | 27,65,n (ESC A n) |
| Dot graphics, n columns | 27,75,LSB,MSB (ESC K) |
| Reset printer | 27,64 (ESC @) |
10 OPEN #2,8,0,"P:"
20 PRINT #2;CHR$(27);"E";"HEADLINE";CHR$(27);"F"
30 PRINT #2;"BACK TO NORMAL TEXT"
40 CLOSE #2
Put codes in the PRINT list, outside quotation marks. "CHR$(27)" in quotes just prints those eight characters. Other printers, including several of Atari's own, use different codes.
7. Dot graphics and a screen dump
In graphics mode, each byte you send prints one column of eight dots. Bit 7 fires the top pin and bit 0 the bottom one. ESC K followed by a two-byte count (low byte first) tells the printer how many columns follow. To dump part of a GRAPHICS 8 screen, read eight pixel rows at a time with LOCATE, build one byte per column, and send each band as one graphics line:
10 DIM D$(20):D$="P:"
20 GRAPHICS 8:COLOR 1:SETCOLOR 2,0,0
30 PLOT 0,0:DRAWTO 159,0:DRAWTO 159,31:DRAWTO 0,31:DRAWTO 0,0
40 DRAWTO 159,31:PLOT 0,31:DRAWTO 159,0
50 FOR X=20 TO 140 STEP 20:PLOT X,4:DRAWTO X,27:NEXT X
60 PRINT "DUMPING 160 X 32 PIXELS..."
70 OPEN #1,8,0,D$
80 PRINT #1;CHR$(27);"A";CHR$(8);
90 FOR Y=0 TO 24 STEP 8
100 PRINT #1;CHR$(27);"K";CHR$(160);CHR$(0);
110 FOR X=0 TO 159:B=0
120 FOR K=0 TO 7:LOCATE X,Y+K,C:B=B+B+C:NEXT K
130 IF B=155 THEN B=154
140 PRINT #1;CHR$(B);:NEXT X
150 PRINT #1:NEXT Y
160 PRINT #1;CHR$(27);"@";:CLOSE #1
170 PRINT "DONE"
Line 80 sets the line spacing to 8/72 inch, so the eight-dot bands touch with no white gaps. Line 120 shifts each pixel in from the top, so the top row lands in bit 7. Line 150 ends each band, and line 160 resets the printer.
Line 130 is the Atari-specific part. A graphics byte that happens to equal 155 is the ATASCII end-of-line. The P: handler pads the rest of the block with blanks, and the 850 turns the 155 into a carriage return and throws away the rest of the block. That damages the graphics line. Changing 155 to 154 loses one dot from that column but keeps everything else intact. Commercial screen-dump programs and printer drivers had to deal with the same problem.
LOCATE is slow, so a full 320 × 192 screen takes a long time in BASIC; machine-language dump programs exist for that.
8. Printing without a printer
- To a disk file. Open
D:FILENAMEinstead ofP:, orLIST "D:PROG.LST"for a listing. The file holds exactly what the printer would have received, with ATASCII EOLs (155) at line ends. Convert those to CR or LF when you move the file to a PC. - FujiNet can pose as a printer on the SIO bus. Its emulations include the Atari 820, 822, 825, 1020, 1025, 1027 and 1029, an Epson FX-80, an Okimate 10, an HTML printer and an ATASCII printer that embeds the Atari font to reproduce listings perfectly. You collect the output through the printer icon on FujiNet's web page.
- Altirra emulates the 820, 825, 1020, 1025 and 1029 printers and an 850's printer port. Text sent to P: appears in its printer output pane, and a file writer device can capture printer-port output in a file.
- atari800 can pass P: output to a host command; its configuration file holds a
PRINT_COMMANDsetting.
Troubleshooting
- Text starts in column 41. An
LPRINTended with ; or ,. UseOPENandPRINT #instead. - Every line overprints the previous one. The 850 sends CR without LF. Turn on the printer's auto line feed switch.
- ERROR 138 (device timeout) on OPEN or LPRINT. Nothing answered as P:. Check the cables and power. An 850 answers as P: only when it senses a printer that is switched on, so turn the printer on first.
- ERROR 139 in the middle of a job. The printer took too long to accept a block, often after several carriage movements in one block. Pad with
CHR$(0)or simplify the codes. - Stray characters such as
EorKon the paper. The ESC didn't arrive, usually becauseCHR$(27)is inside quotation marks. - Holes or junk in printed graphics. A byte of 155 in the data. See line 130.
- ERROR 129. The channel is still open from an earlier run. Add
CLOSE #2before theOPEN.
Further reading
- Atari 850 Interface Module Technical Manual (Atari): Section 12 and the principles-of-operation chapter cover printer port timing, EOL translation and the LPRINT caveat.
- Atari XMM801 Dot-Matrix Graphics Printer Owner's Manual (Atari, 1985): every control code with BASIC examples, including dot-graphics butterflies.
- The Epson Connection: Atari XE/XL Edition by Leigh Edward Zeitz (Merdyne, 1986): a whole book on driving Epson printers from the Atari.
- Mapping the Atari by Ian Chadwick (revised edition, COMPUTE! Books, 1985): locations 28–30 (printer timeout and buffer), 201 (PTABW) and 960–999 (PRNBUF), including a GRAPHICS 0 text-screen dump.
- On this site: CIO and IOCBs, Working with Disk Files in Atari BASIC and FujiNet Setup Guide.
Credits
- Picture: The screen-dump example draws a 160 × 32 pixel picture and sends it to the printer as four bands of dot-graphics data. — Program authors; screenshot by the site
- Atari 850 Interface Module Technical Manual — printer port, block output, EOL translation, LPRINT note, status codes — Internet Archive
- Atari XMM801 Dot-Matrix Graphics Printer Owner's Manual (1985) — PRINT#/OPEN, control codes, ESC K graphics — Internet Archive, 1985
- Atari 400/800 Operating System Source Listing, Rev. A — printer handler (PHWRIT, BLFILL, PHCLOS, PRMODE) — ksquiggle
- Mapping the Atari (revised edition) — 28-30, 201 PTABW, 960-999 PRNBUF — AtariArchives.org
- FujiNet wiki — About Each Printer, Epson FX-80, ATASCII Printer, HTML Printer — FujiNet project
- Altirra release notes — printer devices, 850 printer port, file writer, printer pane — Screenshot from Avery Lee's Altirra page, virtualdub.org
- atari8bit.net book database — The Epson Connection Atari XE/XL Edition (Merdyne 1986) — Atari8bit.net, 1986
- Wikipedia — ESC/P (ESC E / ESC F bold on Epson) — Wikipedia contributors
