{"id":848,"date":"2016-01-22T02:12:14","date_gmt":"2016-01-22T10:12:14","guid":{"rendered":"http:\/\/www.pagetable.com\/?p=848"},"modified":"2016-01-22T02:12:14","modified_gmt":"2016-01-22T10:12:14","slug":"macross-6502-an-assembler-for-people-who-hate-assembly-language","status":"publish","type":"post","link":"https:\/\/www.pagetable.com\/?p=848","title":{"rendered":"Macross 6502, an assembler for people who hate assembly language"},"content":{"rendered":"<p>There are many MOS 6502 cross-assemblers available. Here&#8217;s a new one. Or actually a very old one. &#8220;Macross&#8221;, a very powerful 6502 macro assembler, which was used to create Habitat, Maniac Mansion and Zak McKracken, was developed between 1984 and 1987 at Lucasfilm Ltd. and is now Open Source (MIT license):<\/p>\n<p><a href=\"https:\/\/github.com\/Museum-of-Art-and-Digital-Entertainment\/macross\">https:\/\/github.com\/Museum-of-Art-and-Digital-Entertainment\/macross<\/a><\/p>\n<h2>Some History<\/h2>\n<p>Starting in 1984, a team at Lucasfilm Ltd. was developing one of the first online role-playing games, originally called &#8220;Microcosm&#8221;, which was released as &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Habitat_(video_game)\">Habitat<\/a>&#8221; in 1986 and later renamed to &#8220;Club Caribe&#8221;. The client ran on a Commodore 64, which conntected to the central server through the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Quantum_Link\">Quantum Link<\/a> network.<\/p>\n<p>The client software was developed on a 68K-based Sun workstation running the SunOS variant of Unix using cross-development tools developed by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Chip_Morningstar\">Chip Morningstar<\/a> (who was also the Habitat lead): The &#8220;Macross&#8221; assembler and the &#8220;Slinky&#8221; linker. They were used on every 6502 (Atari 400\/800, Commodore 64, and Apple II) game produced at Lucasfilm Games, from 1984 up until those machines ceased to be relevant to the games market<a href=\"http:\/\/www.pagetable.com\/?p=848#comment-205812\">*<\/a>.<\/p>\n<p>In 2014, <a href=\"http:\/\/themade.org\/\">The Museum of Art and Digital Entertainment<\/a> got a hold of<\/p>\n<ul>\n<li>the source of the original development tools (Macross\/Slinky)<\/li>\n<li>the source of the C64 client<\/li>\n<li>the source of the server (written in PL\/I)<\/li>\n<li>lots of documentation and development logs<\/li>\n<\/ul>\n<p>which originated from an archive created in 1987 in the context of the technology transfer to Fujitsu, which bought all Habitat assets.<\/p>\n<p>Since Macross and Slinky were written for Unix, it was easy to get them compiling with modern compilers (K&#038;R syntax notwithstanding) and running on a modern Unix system. At the time of writing, the code in the <a href=\"https:\/\/github.com\/Museum-of-Art-and-Digital-Entertainment\/macross\">official repository<\/a> has been fixed to compile with clang on OS X. Further fixes and cleanups are very welcome.<\/p>\n<h2>Compiling Macross<\/h2>\n<p>Enter &#8220;make&#8221; both in the toplevel directory and in the &#8220;slinky&#8221; directory, then copy &#8220;macross&#8221; and &#8220;slinky&#8221; into your path. There are <tt>man<\/tt> files in the &#8220;doc&#8221; directory that you may want to install as well.<\/p>\n<h2>Writing Code<\/h2>\n<p>The syntax of Macross source files is very different from modern 6502 cross assembler, and more similar to Commodore&#8217;s own &#8220;A65&#8221; assembler. Here is a small &#8220;Hello World&#8221; for the C64:<\/p>\n<pre>\ndefine strout = 0xab1e\n\nhello:\n    lda #\/text\n    ldy #?text\n    jmp strout\n\ntext:\n    byte \"HELLO WORLD!\", 0\n<\/pre>\n<p>As you can see, hex values have to be specified in C notation (binary is prefixed with &#8220;0b&#8221;), and the operators to extract the low and high bytes of a 16 bit value are &#8220;\/&#8221; and &#8220;?&#8221;, respectively.<\/p>\n<p>Compile and link the source file like this:<\/p>\n<pre>\nmacross -c -o hello.o hello.m\nslinky -e -o hello.bin -n -m hello.sym -l 0xc000 hello.o\ndd if=hello.bin bs=1 skip=2 count=2 of=hello.prg\ndd if=hello.bin bs=1 skip=6 >> hello.prg\n<\/pre>\n<p>The &#8220;dd&#8221; lines convert Slinky&#8217;s output, which is a &#8220;standard a65-style object file&#8221; (which has a header of FF FF, followed by the start address, followed by the end address) into a C64 .PRG file that is only prefixed by the start address.<\/p>\n<p>Here is a slightly more complex example:<\/p>\n<pre>\ndefine bsout = 0xffd2\n\nhello:\n    ldx #0\n    do {\n        lda x[text]\n        cmp #'A'\n        if (geq) {\n            tay\n            iny\n            tya\n        }\n        inx\n        jsr bsout\n    } while (!zero)\n\n    rts\n\ntext:\n    byte \"HELLO WORLD!\", 0\n<\/pre>\n<p>Macross supports C-style if\/else, while and do\/while, as well as do\/until, where the condition can be one of:<\/p>\n<ul>\n<li>zero\/equal<\/li>\n<li>carry<\/li>\n<li>lt\/leq\/gt\/geq<\/li>\n<li>slt\/sleq\/sgt\/sgeq<\/li>\n<li>positive\/plus\/negative\/minus<\/li>\n<li>overflow<\/li>\n<\/ul>\n<p>&#8230;as well as their negated versions.<\/p>\n<p>Also note that the &#8220;absolute, x-indexed&#8221; addressing mode has a different syntax than commonly used.<\/p>\n<h2>Macros<\/h2>\n<p>Macross has a very powerfull macro language. Here is an example:<\/p>\n<pre>\norg 0xc000\n\nfunction makeFirstByte(operand) {\n    mif (isImmediateMode(operand)) {\n        freturn(\/operand)\n    } melse {\n        freturn(operand)\n    }\n}\n\nfunction makeSecondByte(operand) {\n    mif (isImmediateMode(operand)) {\n        freturn(?operand)\n    } melse {\n        freturn(operand + 1)\n    }\n}\n\nmacro movew src, dst {\n    lda makeFirstByte(src)\n    sta makeFirstByte(dst)\n    lda makeSecondByte(src)\n    sta makeSecondByte(dst)\n}\n\nmacro hook_vector index, new, dst {\n    ldx #index * 2\n    movew x[0x0300], dst\n    movew #new, x[0x0300]\n}\n\ndefine VECTOR_INDEX_IRQ = 10\n\n    hook_vector VECTOR_INDEX_IRQ, irq, return + 1\n    rts\n\nirq:\n    inc 0xd020\nreturn:\n    jmp 0xffff\n<\/pre>\n<p>The &#8220;hook_vector&#8221; line will emit the following assembly code:<\/p>\n<pre>\n    ldx #$14\n    lda $0300,x\n    sta $C01D\n    lda $0301,x\n    sta $C01E\n    lda #$19\n    sta $0300,x\n    lda #$C0\n    sta $0301,x\n<\/pre>\n<p>(The example is a little contrived, since adding the index could have been done at assembly time, but the example nicely demonstrates that macros can preserve addressing modes.)<\/p>\n<p>The file <a href=\"https:\/\/github.com\/Museum-of-Art-and-Digital-Entertainment\/macross\/blob\/master\/doc\/macros.itr\">doc\/macros.itr<\/a> contains many useful macros. they are documented in <a href=\"https:\/\/github.com\/Museum-of-Art-and-Digital-Entertainment\/macross\/blob\/master\/doc\/genmacros.itr\">doc\/genmacros.itr<\/a>.<\/p>\n<h2>Full Documentation<\/h2>\n<p>The complete documentation of Macross is available in the file <a href=\"https:\/\/github.com\/Museum-of-Art-and-Digital-Entertainment\/macross\/blob\/master\/doc\/writeup_6502.itr\">doc\/writeup_6502.itr<\/a> in the repository. It is in troff format and can viewed like this:<\/p>\n<pre>\nnroff -ms doc\/writeup_6502.itr\n<\/pre>\n<h2>Future<\/h2>\n<p>Macross is a very different approach to 6502 development, and with the source available, I think it&#8217;s a viable project that should be continued.<\/p>\n<p>I will happily accept pull requests for compile fixes (GCC, VS, &#8230;), cleanups (C99, converting docs from troff to markdown, &#8230;) and features (BIN and PRG output, support for more a modern notation, PETSCII, &#8230;).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many MOS 6502 cross-assemblers available. Here&#8217;s a new one. Or actually a very old one. &#8220;Macross&#8221;, a very powerful 6502 macro assembler, which was used to create Habitat, Maniac Mansion and Zak McKracken, was developed between 1984 and 1987 at Lucasfilm Ltd. and is now Open Source (MIT license): https:\/\/github.com\/Museum-of-Art-and-Digital-Entertainment\/macross Some History Starting &#8230; <a title=\"Macross 6502, an assembler for people who hate assembly language\" class=\"read-more\" href=\"https:\/\/www.pagetable.com\/?p=848\" aria-label=\"Read more about Macross 6502, an assembler for people who hate assembly language\">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,16],"tags":[],"class_list":["post-848","post","type-post","status-publish","format-standard","hentry","category-2","category-archeology","category-github"],"_links":{"self":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/848","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=848"}],"version-history":[{"count":0,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/848\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}