duskos

dusk os fork
git clone git://git.alexwennerberg.com/duskos
Log | Files | Refs | README | LICENSE

arm.fs (3190B) - raw


      1 \ ARM assembler
      2 ?f<< /lib/math.fs
      3 ?f<< /lib/endian.fs
      4 ?f<< /asm/label.fs
      5 
      6 \ RSP=rSP (r13)
      7 \ PSP=r10
      8 \ PS Top of Stack=r9
      9 
     10 : ,) ( op -- ) le, ;
     11 
     12 : cond doer , does> @ 28 lshift swap $0fffffff and or ;
     13 0 cond eq)         1 cond ne)          2 cond cs)          3 cond cc)
     14 0 cond z)          1 cond nz)          2 cond hs)          3 cond lo)
     15 4 cond mi)         5 cond pl)          6 cond vs)          7 cond vc)
     16 8 cond hi)         9 cond ls)          10 cond ge)         11 cond lt)
     17 12 cond gt)        13 cond le)         14 cond al)
     18 
     19 : op doer , does> @ 20 lshift al) ;
     20 $00 op and)        $02 op eor)         $04 op sub)         $06 op rsb)
     21 $08 op add)        $0a op adc)         $0c op sbc)         $0e op rsc)
     22 $11 op tst)        $13 op teq)         $15 op cmp)         $17 op cmn)
     23 $18 op orr)        $1a op mov)         $1c op bic)         $1e op mvn)
     24 
     25 : f) $00100000 or ;
     26 
     27 : #? ( n -- immop rest )
     28   r! dup if log2 then 7 - max0 dup 1 and if 1+ then ( shift ) \ V1=n
     29   r@ over rshift ( shift b )
     30   2dup swap lshift r> -^ ( shift b rest )
     31   rot neg $1e and 7 lshift rot or ( rest imm ) swap ;
     32 
     33 : imm) ( op n -- op )
     34   #? if abort" invalid immediate value" then or $02000000 or ;
     35 
     36 \ Single Data Transfer (LDR, STR, SWP)
     37 $05000000 al) const str)     str) $00100000 or const ldr)
     38 $01000090 al) const swp)
     39 
     40 : pre) $01000000 or ;
     41 : post) $feffffff and ;
     42 : 12b# ( n -- ) $fffff000 and if abort" invalid offset" then ;
     43 : +i) ( op n -- op ) dup 12b# or $00800000 or ;
     44 : -i) ( op n -- op ) dup 12b# or $ff7fffff and ;
     45 : +r) ( op r -- op ) +i) $02000000 or ;
     46 : -r) ( op r -- op ) -i) $02000000 or ;
     47 : 8b) $00400000 or ;
     48 : !) $00200000 or ;
     49 
     50 \ Multiply
     51 $90 al) const mul)
     52 : ismul? ( op -- f ) $0fcf00f0 and $90 = ;
     53 : acc) ( op r -- op ) 12 lshift or $00200000 or ;
     54 
     55 \ Parameters
     56 0 const r0         1 const r1          2 const r2          3 const r3
     57 4 const r4         5 const r5          6 const r6          7 const r7
     58 8 const r8         9 const r9          10 const r10        11 const r11
     59 12 const r12       13 const r13        14 const r14        15 const r15
     60                    9 const rTOP        10 const rPSP       11 const rA
     61 12 const rIP       13 const rSP        14 const rLR        15 const rPC
     62 : rn) ( op r -- op ) 16 lshift or ;
     63 : rd) ( op r -- op ) over ismul? 4 * 12 + lshift or ;
     64 : rs) ( op r -- op ) 8 lshift or ;
     65 : rm) ( op r -- op ) or ;
     66 : rdn) tuck rd) swap rn) ;
     67 
     68 \ immediate shift operations ( op n -- op )
     69 : _ ( op n type -- op ) dip 3 lshift | << or 4 lshift or ;
     70 : lsl) 0 _ ; : lsr) 1 _ ; : asr) 2 _ ; : ror) 3 _ ;
     71 \ register shift operations ( op r -- op )
     72 : _ ( op r type -- op ) dip 4 lshift | << 1+ or 4 lshift or ;
     73 : rlsl) 0 _ ; : rlsr) 1 _ ; : rasr) 2 _ ; : rror) 3 _ ;
     74 
     75 \ Branching
     76 : _rel>off >> >> 2 - $ffffff and ;
     77 : b) ( rel32 -- op ) _rel>off $ea000000 or ;
     78 : bl) b) $01000000 or ;
     79 : bx) ( r -- op ) $e12fff10 or ;
     80 
     81 : forward! ( jmpaddr -- )
     82   here over - _rel>off over le@ $ff000000 and or swap le! ;
     83 
     84 \ Macros
     85 
     86 : push, ( r -- ) str) swap rd) rSP rn) CELLSZ -i) pre) !) ,) ;
     87 : pop, ( r -- ) ldr) swap rd) rSP rn) CELLSZ +i) post) ,) ;
     88 : ppush, ( r -- ) str) swap rd) rPSP rn) CELLSZ -i) pre) !) ,) ;
     89 : ppop, ( r -- ) ldr) swap rd) rPSP rn) CELLSZ +i) post) ,) ;
     90