This chapter documents the Backend for the TR3200 cpu.

1 Legal
=======

This module is written in 2014 by Luis Panadero Guardeño and is covered
by the vasm copyright without modifications.

2 General
=========

This backend accepts TR3200 instructions as described in the TR3200
specification (https://github.com/trillek-team/trillek-computer)

   The target address type is 32 bits.

   Default alignment for sections is 4 bytes.  Instructions alignment is
4 bytes.  Data is aligned to its natural alignment by default, i.e.  2
byte wide data alignment is 2 bytes and 4 byte wide data alignment is 4
byte.

   The backend uses TR3200 syntax!  This means the left operands are
always the destination and the right operand is the source (except for
single operand instructions).  Register names have to be prefixed by a
'‘%’' (‘%bp’, ‘%r0’, etc.)  This means that it should accept WaveAsm
assembly files if oldstyle syntax module is being used.  The
instructions are lowercase, ‘-dotdir’ option is being used and
directives are not in the first column.

3 Extensions
============

Predefined register symbols in this backend:

   − register by number: ‘r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13
     r14 r15’
   − special registers by name: ‘bp sp y ia flags’

4 Known Problems
================

Some known problems of this module at the moment:

   − This module need to be fully checked, but has been tested building
     a full program that could be found here :
     <https://github.com/Zardoz89/trillek-firmware>

   − Instruction relocations are missing.

5 Error Messages
================

This module has the following error messages:

   − 2001: illegal operand
   − 2002: illegal qualifier <%s>
   − 2003: data size not supported

6 Example
=========

It follows a little example to illustrate TR3200 assembly using the
oldstyle syntax module (option ‘-dotdir’ required):

     const   .equ 0xBEBACAFE           ; A constant
     an_addr .equ 0x100                ; Other constant

     ; ROM code
           .org 0x100000
           .text
     _start                            ; Label with or without a ending ":"
           mov %sp, 0x1000             ; Set the initial stack

           mov %r0, 0
           mov %r1, 0xA5
           mov %r2, 0
           storeb %r0, an_addr, %r1

           add %r0, %r2, %bp
           add %r0, %r2, 0
           add %r0, %r0, 10
           add %r0, %r0, 10h
           add %r0, %r0, 0x100010
           add %r0, %r0, (256 + 100)   ; vasm parses math expressions

           mov %r2, 0
           mov %r3, 0x10200

           loadb %r6, 0x100200
           loadb %r1, %r2, 0x100200
           loadb %r1, %r2, %r3
           loadb %r4, var1

           push %r0
           .repeat 2                 ; directives to repeat stuff!
           push const
           .endrepeat

           .repeat 2
           pop %r5
           .endrepeat
           pop %r0

           rcall foo                 ; Relative call/jump!
           sleep

     foo:      ; Subrutine
           ifneq %r5, 0
             mul %r5, %r5, 2
           sub %r5, %r5, 1

           ret

     ; ROM data
           .org 0x100500
     var1  .db 0x20                  ; A byte size variable
           .even                     ; Enforce to align to even address
     var3  .dw 0x1020                ; A word size variable
     var4  .dd 0x0A0B0C20            ; A double word size variable
     str1  .asciiz "Hello world!"    ; ASCII string with null termination
     str2  .string "Hello world!"    ; ASCII string with null termination
           .fill 5, 0xFF             ; Fill 5 bytes with 0xFF
           .reserve 5                ; Reserves space for 5 byte

