Source

dcpu-16-forth / macros.dcpu.inc

; Macros taken from http://board.flatassembler.net/topic.php?p=142613#142613
A equ eax
B equ ebx
C equ ecx
X equ edx
Y equ ebp
Z equ esp
I equ esi
J equ edi

@A = 0
@B = 1
@C = 2
@X = 3
@Y = 4
@Z = 5
@I = 6
@J = 7

@SET = 0x1
@ADD = 0x2
@SUB = 0x3
@MUL = 0x4
@DIV = 0x5
@MOD = 0x6
@SHL = 0x7
@SHR = 0x8
@AND = 0x9
@BOR = 0xA
@XOR = 0xB
@IFE = 0xC
@IFN = 0xD
@IFG = 0xE
@IFB = 0xF

@JSR = 0x01

macro parse_operand value {
   define v value
   @nextword = -1
   match =POP,v \{
     define v
     @value = 0x18
   \}
   match =PEEK,v \{
     define v
     @value = 0x19
   \}
   match =PUSH,v \{
     define v
     @value = 0x1A
   \}
   match =SP,v \{
     define v
     @value = 0x1B
   \}
   match =PC,v \{
     define v
     @value = 0x1C
   \}
   match =O,v \{
     define v
     @value = 0x1D
   \}
   match [addr],v \{
     define v
     if +addr relativeto 0
       @value = 0x1E
       @nextword = word addr
     irps reg,A B C X Y Z I J \\{
       else if +addr relativeto reg
         if +addr-reg = 0
           @value = 0x08 + @\\#reg
         else
           @value = 0x10 + @\\#reg
           @nextword = +addr-reg
         end if
     \\}
     end if
   \}
   match expr,v \{
     define v
     if +expr relativeto 0
       if expr>=0 & expr<=0x1F
         @value = 0x20 + expr
       else
         @value = 0x1F
         @nextword = word expr
       end if
     irps reg,A B C X Y Z I J \\{
       else if +expr relativeto reg
         assert +expr-reg = 0
         @value = 0x00 + @\\#reg
     \\}
     end if
   \}
}

macro basic_instruction opcode,a,b {
  @word1 = opcode and 1111b
  parse_operand a
  @word1 = @word1 or @value shl 4
  @word2 = @nextword
  parse_operand b
  @word1 = @word1 or @value shl (4+6)
  @word3 = @nextword
  db @word1 shr 8,@word1 and 0FFh
  if @word2>=0
    db @word2 shr 8,@word2 and 0FFh
  end if
  if @word3>=0
    db @word3 shr 8,@word3 and 0FFh
  end if
}

macro nonbasic_instruction opcode,a {
  @word1 = (opcode and 111111b) shl 4
  parse_operand a
  @word1 = @word1 or @value shl (4+6)
  db @word1 shr 8,@word1 and 0FFh
  if @nextword>=0
    db @nextword shr 8,@nextword and 0FFh
  end if
}

irps instr,SET ADD SUB MUL DIV MOD SHL SHR AND BOR XOR IFE IFN IFG IFB {
  macro instr a,b \{ basic_instruction @\#instr,a,b \}
}

irps instr,JSR {
  macro instr a \{ nonbasic_instruction @\#instr,a \}
}

macro LABEL name {
  ; display "Defining label ", `name, 10
  name = $ shr 1
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.