{"id":901,"date":"2017-07-10T14:45:08","date_gmt":"2017-07-10T21:45:08","guid":{"rendered":"http:\/\/www.pagetable.com\/?p=901"},"modified":"2017-07-10T14:45:08","modified_gmt":"2017-07-10T21:45:08","slug":"80-columns-text-on-the-commodore-64","status":"publish","type":"post","link":"https:\/\/www.pagetable.com\/?p=901","title":{"rendered":"80 Columns Text on the Commodore 64"},"content":{"rendered":"<p>The text screen of the Commodore 64 has a resolution of 40 by 25 characters, based on the hardware text mode of the VIC-II video chip. This is a step up from the VIC-20&#8217;s 22 characters per line, but since computers in the professional segment (Commodore PET 8000 series, CP\/M, MS-DOS) usually had 80 columns, several solutions \u2013 both <a href=\"https:\/\/www.youtube.com\/watch?v=BJzOErvJwZs\">hardware<\/a> and <a href=\"http:\/\/mikenaberezny.com\/hardware\/projects\/c64-soft80\">software<\/a> \u2013 exist to allow 80 columns on a C64 as well. Let&#8217;s look at how this is done in software! At the end of this article, I present a fast and full-featured open source implementation with several different character sets.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"404\" height=\"293\" src=\"docs\/80columns\/c64.png\"\/><\/p>\n<h2>Regular 40&#215;25 Mode<\/h2>\n<p>First, we need to understand how 40 columns are done. The VIC-II video chip has dedicated support for a text mode. There is a 1000 (= 40 * 25) byte &#8220;Screen RAM&#8221;, each byte of which contains the character to be displayed at that location in the form of an index into the 2 KB character set, which contains 8 bytes (for 8&#215;8 pixels) for each of the 256 characters. In addition, there is a &#8220;Color RAM&#8221;, which contains a 1000 (again 40 * 25) 4-bit values, which represents a color for each character on the screen.<\/p>\n<p>Putting a character onto the screen is quite trivial: Just write the index of it to offset <i>column<\/i> + 40 * <i>line<\/i> into Screen RAM, and its color to the same offset in Color RAM. An application can load its own character set, but the Commodore 64 already comes with two character sets in ROM: One with uppercase characters and lots of graphical symbols (&#8220;GRAPHICS&#8221;), and one with upper and lower case (&#8220;TEXT&#8221;). You can switch these by pressing the Commodore and the Shift key at the same time.<\/p>\n<h2>Bitmap Mode<\/h2>\n<p>There is no hardware support for an 80 column mode, but such a mode can be implemented in software by using bitmap mode. In bitmap mode, all 320 by 200 pixels on the screen can be freely addressed through an 8000 byte bitmap, which contains one bit for every pixel on the screen. Luckily for us, the layout of the bitmap in memory is not linear, but reminds of the encoding of text mode: The first 8 bytes in Bitmap RAM don&#8217;t describe, as you would expect, the leftmost 64 pixels on the first line. Instead, they describe the top left 8&#215;8 block. The next 8 bytes describe the 8&#215;8 block to the right of it, and so on.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"518\" height=\"518\" src=\"docs\/80columns\/bitmap.png\"\/><\/p>\n<p>This is the same layout as the character set&#8217;s: The first 8 bytes correspond to the first character, the next 8 bytes to the second character and so on. Drawing an 8&#215;8 character onto a bitmap (aligned to the 8&#215;8 grid) is as easy as copying 8 consecutive bytes.<\/p>\n<p>This is what an 8&#215;8 font looks like in memory:<\/p>\n<pre>\n0000 \u00b7\u00b7\u2588\u2588\u2588\u2588\u00b7\u00b7  0008 \u00b7\u00b7\u00b7\u2588\u2588\u00b7\u00b7\u00b7  0010 \u00b7\u2588\u2588\u2588\u2588\u2588\u00b7\u00b7\n0001 \u00b7\u2588\u2588\u00b7\u00b7\u2588\u2588\u00b7  0009 \u00b7\u00b7\u2588\u2588\u2588\u2588\u00b7\u00b7  0011 \u00b7\u2588\u2588\u00b7\u00b7\u2588\u2588\u00b7\n0002 \u00b7\u2588\u2588\u00b7\u2588\u2588\u2588\u00b7  000a \u00b7\u2588\u2588\u00b7\u00b7\u2588\u2588\u00b7  0012 \u00b7\u2588\u2588\u00b7\u00b7\u2588\u2588\u00b7\n0003 \u00b7\u2588\u2588\u00b7\u2588\u2588\u2588\u00b7  000b \u00b7\u2588\u2588\u2588\u2588\u2588\u2588\u00b7  0013 \u00b7\u2588\u2588\u2588\u2588\u2588\u00b7\u00b7\n0004 \u00b7\u2588\u2588\u00b7\u00b7\u00b7\u00b7\u00b7  000c \u00b7\u2588\u2588\u00b7\u00b7\u2588\u2588\u00b7  0014 \u00b7\u2588\u2588\u00b7\u00b7\u2588\u2588\u00b7\n0005 \u00b7\u2588\u2588\u00b7\u00b7\u00b7\u2588\u00b7  000d \u00b7\u2588\u2588\u00b7\u00b7\u2588\u2588\u00b7  0015 \u00b7\u2588\u2588\u00b7\u00b7\u2588\u2588\u00b7\n0006 \u00b7\u00b7\u2588\u2588\u2588\u2588\u00b7\u00b7  000e \u00b7\u2588\u2588\u00b7\u00b7\u2588\u2588\u00b7  0016 \u00b7\u2588\u2588\u2588\u2588\u2588\u00b7\u00b7\n0007 \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7  000f \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7  0017 \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\n<\/pre>\n<p>For an 80 column screen, every character is 4&#215;8 pixels. So we could describe the character set like this:<\/p>\n<pre>\n0000 \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7  0008 \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7  0010 \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\n0001 \u00b7\u00b7\u2588\u00b7\u00b7\u00b7\u00b7\u00b7  0009 \u00b7\u00b7\u2588\u00b7\u00b7\u00b7\u00b7\u00b7  0011 \u00b7\u2588\u2588\u00b7\u00b7\u00b7\u00b7\u00b7\n0002 \u00b7\u2588\u00b7\u2588\u00b7\u00b7\u00b7\u00b7  000a \u00b7\u2588\u00b7\u2588\u00b7\u00b7\u00b7\u00b7  0012 \u00b7\u2588\u00b7\u2588\u00b7\u00b7\u00b7\u00b7\n0003 \u00b7\u2588\u2588\u2588\u00b7\u00b7\u00b7\u00b7  000b \u00b7\u2588\u2588\u2588\u00b7\u00b7\u00b7\u00b7  0013 \u00b7\u2588\u2588\u00b7\u00b7\u00b7\u00b7\u00b7\n0004 \u00b7\u2588\u2588\u2588\u00b7\u00b7\u00b7\u00b7  000c \u00b7\u2588\u00b7\u2588\u00b7\u00b7\u00b7\u00b7  0014 \u00b7\u2588\u00b7\u2588\u00b7\u00b7\u00b7\u00b7\n0005 \u00b7\u2588\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7  000d \u00b7\u2588\u00b7\u2588\u00b7\u00b7\u00b7\u00b7  0015 \u00b7\u2588\u00b7\u2588\u00b7\u00b7\u00b7\u00b7\n0006 \u00b7\u00b7\u2588\u2588\u00b7\u00b7\u00b7\u00b7  000e \u00b7\u2588\u00b7\u2588\u00b7\u00b7\u00b7\u00b7  0016 \u00b7\u2588\u2588\u00b7\u00b7\u00b7\u00b7\u00b7\n0007 \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7  000f \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7  0017 \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\n<\/pre>\n<p>Every 4&#215;8 character on the screen is either in the left half or the right half of an 8&#215;8 block, so drawing an 4&#215;8 character is as easy as copying the bit pattern into the 8&#215;8 block \u2013 and shifting it 4 bits to the right for characters at odd positions.<\/p>\n<h2>Color<\/h2>\n<p>In bitmap mode, it is only possible to use two out of the 16 colors per 8&#215;8 block, because there are only 1000 (40 * 25) entries for the color matrix. This is a problem, since we need three colors per 8&#215;8: Two for the two characters and one for the background. We will have to compromise: Whenever a character gets drawn into an 8&#215;8 block and the other character in the block has a different color, that other character will be changed to the same color as the new character.<\/p>\n<h2>Scrolling<\/h2>\n<p>Scrolling on a text screen is easy: 960 bytes of memory have to be copied to move the character indexes to their new location. In bitmap mode, 7680 bytes have to be copied &#8211; 8 times more. Even with the most optimized implementation (73ms, about 3.5 frames), scrolling will be slower, and tearing artifacts are unavoidable.<\/p>\n<h2>Character Set<\/h2>\n<p>Creating a 4&#215;8 character set that is both readable and looks good is not easy. There has to be a one-pixel gap between characters, so characters can effectively only be 3 pixels wide. For characters like &#8220;M&#8221; and &#8220;N&#8221;, this is a challenge.<\/p>\n<p>These are the character sets of four different software solutions for 80 columns:<\/p>\n<p><b>COLOR 80 by Richvale Telecommunications<\/b><br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"188\" height=\"64\" src=\"docs\/80columns\/g2.png\"\/>&nbsp;&nbsp;<img loading=\"lazy\" decoding=\"async\" width=\"188\" height=\"64\" src=\"docs\/80columns\/t2.png\"\/><\/p>\n<p><b>80COLUMNS<\/b><br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"188\" height=\"64\" src=\"docs\/80columns\/g1.png\"\/>&nbsp;&nbsp;<img loading=\"lazy\" decoding=\"async\" width=\"188\" height=\"64\" src=\"docs\/80columns\/t1.png\"\/><\/p>\n<p><b>SCREEN-80 by Compute\u2019s Gazette<\/b><br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"188\" height=\"64\" src=\"docs\/80columns\/g4.png\"\/>&nbsp;&nbsp;<img loading=\"lazy\" decoding=\"async\" width=\"188\" height=\"64\" src=\"docs\/80columns\/t4.png\"\/><\/p>\n<p><b>Highspeed80 by CKtwo<\/b><br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"188\" height=\"64\" src=\"docs\/80columns\/g3.png\"\/>&nbsp;&nbsp;<img loading=\"lazy\" decoding=\"async\" width=\"188\" height=\"64\" src=\"docs\/80columns\/t3.png\"\/><\/p>\n<p>Some observations:<\/p>\n<ul>\n<li>Highspeed80 and SCREEN-80 have capitals of a height of 7 pixels (more detail, but very tight vertical spacing, making it hard to read), while COLOR 80 uses only 5 pixels (more square like the original 8&#215;8 font, but less detail).  6 pixels, as used by 80COLUMNS, seems like a good compromise.<\/li>\n<li>80COLUMNS has the empty column at the left, which makes single characters in reverse mode more readable, since most characters have their stems at the left.<\/li>\n<li>Except for Highspeed80, the graphical symbols are very similar between the different character sets.<\/li>\n<li>All four character sets use the same strategy to distinguish between &#8220;M&#8221; and &#8220;N&#8221;.<\/li>\n<\/ul>\n<h2>The Editor<\/h2>\n<p>The &#8220;EDITOR&#8221; is the part of C64&#8217;s ROM operating system (&#8220;KERNAL&#8221;) that handles printing characters to the screen (and interpreting control characters), as well as converting on-screen contents back into a PETSCII string &#8211; yes, text input on CBM computers is done by feeding the keyboard input directly into character output, and reading back the screen contents when the user presses the return key. This way, the user can use the cursor keys to navigate to existing text anywhere on the screen (even to the output of previous commands), edit it, and have the system interpret it as input when pressing return.<\/p>\n<p>The C64&#8217;s EDITOR can only deal with 40 columns (but has a very nice feature that allows using two 40 character lines as one virtual 80 character input line), and has no idea how to draw into a bitmap, so a software 80 characters solution basically has to provide a complete reimplementation of this KERNAL component.<\/p>\n<p>The KERNAL provides user vectors for lots of its functionality, so both character output, and reading back characters from the screen can be hooked (vectors <tt>IBSOUT<\/tt> at $0326 and <tt>IBASIN<\/tt> at $0324). In addition to drawing characters into the bitmap, the character codes have to be cached in a 80&#215;25 virtual Screen RAM, so the input routine can read them back.<\/p>\n<p>The PETSCII code contains control codes for changing the current color, moving the cursor, clearing the screen, turning reverse on and off, and switching between the &#8220;GRAPHICS&#8221; and &#8220;TEXT&#8221; character sets. The new editor has provide code to interpret these. There are two special cases though: When in quote mode (the user is typing text between quotes) or insert mode (the user has typed shift+delete), most special characters show up as inverted graphical characters instead of being interpreted. This way, control characters can be included e.g. in strings in BASIC programs.<\/p>\n<p>There are two functions though that cannot be intercepted through vectors: Applications (and BASIC programs) change the screen background color by writing the color&#8217;s value to $d020, since there is no KERNAL function or BASIC command for it, and the KERNAL itself switches between the two character sets (when the user presses the Commodore and the Shift key at the same time) by directly writing to $d018. The only way to intercept these is by hooking the timer interrupt vector and detecting a change in these VIC-II registers. If the background color has changed, the whole 1000 byte color matrix for bitmap mode has to be updated, and if the character set has changed, the whole screen has to be redrawn.<\/p>\n<h2>The Implementation<\/h2>\n<p>I looked at all existing software implementations I could find and concluded that &#8220;80COLUMNS&#8221; (by an unknown author) had the best design and was the only one to implement the full feature set of the original EDITOR. I reverse-engineered it into structured, easy to read code, added <a href=\"http:\/\/cbm.ficicilar.name.tr\/uncorrected\/basicv2\/basv2.html\">Ilker Ficicilar&#8217;s fast scrolling patch<\/a> as well as my own minor cleanups, fixes and optimizations.<\/p>\n<p><a href=\"https:\/\/www.github.com\/mist64\/80columns\">https:\/\/www.github.com\/mist64\/80columns<\/a><\/p>\n<p>The project requies <a href=\"https:\/\/github.com\/cc65\/cc65\">cc65<\/a> and <a href=\"https:\/\/github.com\/bitshifters\/exomizer\">exomizer<\/a> to build. Running <tt>make<\/tt> will produce <tt>80columns-compressed.prg<\/tt>, which is about 2.2 KB in size and can be started using <tt>LOAD<\/tt>\/<tt>RUN<\/tt>.<\/p>\n<p>The source contains several character sets (<tt>charset.s<\/tt>, <tt>charset2.s<\/tt> etc.) from different 80 column software solutions, which can be selected by changing the reference to the filename in the <tt>Makefile<\/tt>.<\/p>\n<p>The object code resides at $c800-$cfff. The two character sets are located at $d000-$d7ff. The virtual 80&#215;25 Screen RAM (in order to read back screen contents) is at $c000-$c7ff. The bitmap is at $e000-$ff40, and the color matrix for bitmap mode is at $d800-$dbe8. All this lies beyond the top of BASIC RAM, so BASIC continues to have 38911 bytes free.<\/p>\n<p>In order to speed up drawing, the character set contains all characters duplicated like this:<\/p>\n<pre>\n0000 \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7  0008 \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7  0010 \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\n0001 \u00b7\u00b7\u2588\u00b7\u00b7\u00b7\u2588\u00b7  0009 \u00b7\u00b7\u2588\u00b7\u00b7\u00b7\u2588\u00b7  0011 \u00b7\u2588\u2588\u00b7\u00b7\u2588\u2588\u00b7\n0002 \u00b7\u2588\u00b7\u2588\u00b7\u2588\u00b7\u2588  000a \u00b7\u2588\u00b7\u2588\u00b7\u2588\u00b7\u2588  0012 \u00b7\u2588\u00b7\u2588\u00b7\u2588\u00b7\u2588\n0003 \u00b7\u2588\u2588\u2588\u00b7\u2588\u2588\u2588  000b \u00b7\u2588\u2588\u2588\u00b7\u2588\u2588\u2588  0013 \u00b7\u2588\u2588\u00b7\u00b7\u2588\u2588\u00b7\n0004 \u00b7\u2588\u2588\u2588\u00b7\u2588\u2588\u2588  000c \u00b7\u2588\u00b7\u2588\u00b7\u2588\u00b7\u2588  0014 \u00b7\u2588\u00b7\u2588\u00b7\u2588\u00b7\u2588\n0005 \u00b7\u2588\u00b7\u00b7\u00b7\u2588\u00b7\u00b7  000d \u00b7\u2588\u00b7\u2588\u00b7\u2588\u00b7\u2588  0015 \u00b7\u2588\u00b7\u2588\u00b7\u2588\u00b7\u2588\n0006 \u00b7\u00b7\u2588\u2588\u00b7\u00b7\u2588\u2588  000e \u00b7\u2588\u00b7\u2588\u00b7\u2588\u00b7\u2588  0016 \u00b7\u2588\u2588\u00b7\u00b7\u2588\u2588\u00b7\n0007 \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7  000f \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7  0017 \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\n<\/pre>\n<p>This way, the drawing code only has to mask the value instead of shifting it. In addition, parts of character drawing and all of screen scrolling are using unrolled loops for performance.<\/p>\n<p>Contributions to the project are very welcome. It would be especially interesting to add new character sets, both existing 4&#215;8 fonts from other projects (including hinted TrueType fonts!), and new ones that combine the respective strengths of the existing ones.<\/p>\n<h2>80&#215;33 Mode?<\/h2>\n<p>Reducing the size of characters to 4&#215;6 would allow a text mode resolution of 80&#215;33 characters. <a href=\"http:\/\/www.kludgesoft.com\/c64\/nt10.html\">Novaterm 10<\/a> has an implementation. At this resolution, logical characters don&#8217;t end at vertical 8&#215;8 boundaries any more, making color impossible, and the drawing routine a little slower. It would be interesting to add an 80&#215;33 mode as a compile time option to &#8220;80columns&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The text screen of the Commodore 64 has a resolution of 40 by 25 characters, based on the hardware text mode of the VIC-II video chip. This is a step up from the VIC-20&#8217;s 22 characters per line, but since computers in the professional segment (Commodore PET 8000 series, CP\/M, MS-DOS) usually had 80 columns, &#8230; <a title=\"80 Columns Text on the Commodore 64\" class=\"read-more\" href=\"https:\/\/www.pagetable.com\/?p=901\" aria-label=\"Read more about 80 Columns Text on the Commodore 64\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5,41,8,16],"tags":[],"class_list":["post-901","post","type-post","status-publish","format-standard","hentry","category-2","category-archeology","category-c64","category-commodore","category-github"],"_links":{"self":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=901"}],"version-history":[{"count":0,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/901\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}