Ophis/site/manual/c292.html

344 lines
6.1 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Character maps</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="Programming with Ophis"
HREF="book1.html"><LINK
REL="PREVIOUS"
TITLE="Example code"
HREF="x287.html"><LINK
REL="NEXT"
TITLE="Local variables and memory segments"
HREF="c329.html"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Programming with Ophis</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x287.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="c329.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="CH4-LINK"
></A
>Character maps</H1
><P
> Now we will close the gap between the Commodore's
version of ASCII and the real one. We'll also add a time-delay
routine to slow down the output. This routine isn't really of
interest to us right now, so we'll add a subroutine
called <TT
CLASS="LITERAL"
>delay</TT
> that executes 2,560*(accumulator)
<KBD
CLASS="USERINPUT"
>NOP</KBD
>s. By the time the program is finished,
we'll have executed 768,000 no-ops.
</P
><P
> There actually are better ways of getting a time-delay on the
Commodore 64; we'll deal with those in <A
HREF="c329.html"
>the Chapter called <I
>Local variables and memory segments</I
></A
>.
As a result, there isn't really a lot to discuss here. The later
tutorials will be building off of <A
HREF="x477.html"
><I
><I
>tutor4a.oph</I
></I
></A
>, so you may want to get familiar with
that. Note also the change to the body of
the <TT
CLASS="LITERAL"
>greet</TT
> macro.
</P
><P
> On to the topic at hand. Let's change the code to use mixed case.
We defined the <TT
CLASS="LITERAL"
>upper'case</TT
>
and <TT
CLASS="LITERAL"
>lower'case</TT
> aliases back
in <A
HREF="c236.html"
>the Chapter called <I
>Headers, Libraries, and Macros</I
></A
> as part of the
standard <A
HREF="x469.html"
><I
><I
>kernal.oph</I
></I
></A
>
header, so we can add this before our invocations of
the <TT
CLASS="LITERAL"
>greet</TT
> macro:
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> lda #lower'case
jsr chrout</PRE
></TD
></TR
></TABLE
><P
> And that will put us into mixed case mode. So, now we just need
to redefine the data so that it uses the mixed-case:
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>hello1: .byte "Hello, ",0
hello2: .byte "!", 13, 0
target1: .byte "programmer", 0
target2: .byte "room", 0
target3: .byte "building", 0
target4: .byte "neighborhood", 0
target5: .byte "city", 0
target6: .byte "nation", 0
target7: .byte "world", 0
target8: .byte "Solar System", 0
target9: .byte "Galaxy", 0
target10: .byte "Universe", 0</PRE
></TD
></TR
></TABLE
><P
> The code that does this is in <A
HREF="x481.html"
><I
><I
>tutor4b.oph</I
></I
></A
>. If you assemble and run it, you will
notice that the output is not what we want. In particular, upper
and lowercase are reversed, so we have messages
like <SAMP
CLASS="COMPUTEROUTPUT"
>hELLO, sOLAR sYSTEM!</SAMP
>. For
the specific case of PETSCII, we can just fix our strings, but
that's less of an option if we're writing for the Apple II's
character set, or targeting a game console that puts its letters
in arbitrary locations. We need to remap how strings are turned
into byte values. The <TT
CLASS="LITERAL"
>.charmap</TT
>
and <TT
CLASS="LITERAL"
>.charmapbin</TT
> directives do what we need.
</P
><P
> The <TT
CLASS="LITERAL"
>.charmap</TT
> directive usually takes two
arguments; a byte (usually in character form) indicating the ASCII
value to start remapping from, and then a string giving the new
values. To do our case-swapping, we write two directives before
defining any string constants:
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>.charmap 'A, "abcdefghijklmnopqrstuvwxyz"
.charmap 'a, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"</PRE
></TD
></TR
></TABLE
><P
> Note that the <TT
CLASS="LITERAL"
>'a</TT
> constant in the second
directive refers to the <SPAN
CLASS="QUOTE"
>"a"</SPAN
> character in the source,
not in the current map.
</P
><P
> The fixed code is in <A
HREF="x485.html"
><I
><I
>tutor4c.oph</I
></I
></A
>, and will produce the expected results
when run.
</P
><P
> An alternative is to use a <TT
CLASS="LITERAL"
>.charmapbin</TT
>
directive to replace the entire character map directly. This
specifies an external file, 256 bytes long, that is loaded in at
that point. A binary character map for the Commodore 64 is
provided with the sample programs
as <TT
CLASS="FILENAME"
>petscii.map</TT
>. There are also three
files, <TT
CLASS="FILENAME"
>a2normal.map</TT
>, <TT
CLASS="FILENAME"
>a2inverse.map</TT
>,
and <TT
CLASS="FILENAME"
>a2blink.map</TT
> that handle the Apple II's
very nonstandard character encodings.
</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x287.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="book1.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="c329.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Example code</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Local variables and memory segments</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>