?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.         ;-----------------------------------------------------------------
  2.         ; idea is from NEO SPECTRUMAN, who was trying to speed up "opcode" jumptable.
  3.         ; implementation of Lua scripts and macros for sjasmplus is from Ped7g
  4.             device zxspectrum48
  5.  
  6.         ;-----------------------------------------------------------------
  7.         ; example of usage of the produced table (code provided by NEO SPECTRUMAN)
  8.             org     $C000
  9.         ; A = operation (alias "opcode") number 0..255
  10.             ld      l,a                 ;4
  11.             ld      h,high opJpTab      ;7
  12.             ld      h,(hl)              ;7
  13.             jp      (hl)                ;4
  14.                                         ;=22t
  15.  
  16.         ;-----------------------------------------------------------------
  17.         ; define LUA functions for memory allocations for opcodes functions
  18.         ;
  19.         ; (the ";" ahead of "end" and some "--" is not needed for Lua, but for my text
  20.         ; editor sjasmplus syntax highlight, as it gets confused by lua source)
  21.         ;
  22.         ; Opcodes *must* be allocated in sequence (0,1,2 ...) to avoid large empty
  23.         ; areas in memory, or even running out of memory completely. Also opcode
  24.         ; implementation subroutines must be reasonably short (few bytes, not hundreds)
  25.  
  26.         lua pass1
  27.             function allocateOpMemory(opcode)
  28.                 -- search for free "page" (512B pages starting at opRoutines address)
  29.                 freePage = _c("high opRoutines")
  30.                 while allocatedPages[freePage] and opcode < allocatedPages[freePage] do
  31.                     freePage = freePage + 2
  32.                     -- +2 to operate over 512 bytes, with 256B pages high opcodes like FE
  33.                     -- may overwrite following page where early opcodes like 01 resides
  34.                 ;end
  35.                 ; -- remember it for "finishOpAllocate" function
  36.                 _G.lastFreePage = freePage
  37.                 ; -- free page found, emit it into jump table
  38.                 _pc(string.format("org $%04x", _c("opJpTab") + opcode))
  39.                 _pc(string.format("db $%02x", freePage))
  40.                 ; -- and reset ORG to target memory for opcode function body
  41.                 _pc(string.format("org $%04x", freePage*256 + opcode))
  42.                 _pl(string.format("opcode_%02x_impl:", opcode))
  43.             ;end    -- ";" to make my Kate editor syntax highlight survive "end" in lua
  44.  
  45.             function finishOpAllocate()
  46.                 assert(_G.lastFreePage, "allocateOpMemory not called yet")
  47.                 allocatedPages[_G.lastFreePage] = _c("$ & $1FF")
  48.             ;end
  49.  
  50.             function setOrgAfterLastAllocated()
  51.                 checkPage = _c("high opRoutines")
  52.                 while allocatedPages[checkPage] do
  53.                     lastAdr = checkPage*256 + allocatedPages[checkPage]
  54.                     checkPage = checkPage + 2
  55.                 ;end
  56.                 assert(lastAdr, "no memory was allocated yet")
  57.                 _pc(string.format("org $%04x", lastAdr))
  58.             ;end
  59.         endlua
  60.  
  61.         ;-----------------------------------------------------------------
  62.         ; helper macros to make the lua calls one-liners in asm
  63.         macro allocateOpMemory _opcode?
  64. @__allocateOpMemory_opcode = _opcode?
  65.             lua allpass
  66.                 allocateOpMemory(_c("__allocateOpMemory_opcode"))
  67.             endlua
  68.         endm
  69.         macro finishOpAllocate
  70.             lua allpass
  71.                 finishOpAllocate()
  72.             endlua
  73.         endm
  74.  
  75.         ;-----------------------------------------------------------------
  76.         ; global definitions and variables used to generate jump table
  77.        
  78.         ; jump table with "high" bytes of opcode function addresses
  79. opJpTab     equ     $7F00               ; must be 256B aligned, size 256B
  80.         ; opcode functions will go into memory starting from $8000
  81. opRoutines  equ     $8000               ; must be 256B aligned, size dynamic (N * 512)
  82.         ; reset all Lua global variables ahead of each assembling pass
  83.         lua allpass
  84.             allocatedPages = {}     -- reset allocated pages for every pass
  85.             lastFreePage = nil
  86.         endlua
  87.  
  88.         ;-----------------------------------------------------------------
  89.         ; define opcode functions (builds also jump table and labels like "opcode_a1_impl")
  90.  
  91.             allocateOpMemory 0
  92.             db      1, 2            ; fake "implementation" (just 1,2,3,4,... byte values)
  93.             finishOpAllocate
  94.  
  95.             allocateOpMemory 1
  96.             db      3, 4, 5
  97.             finishOpAllocate
  98.  
  99.             allocateOpMemory 2
  100.             db      6, 7, 8
  101.             finishOpAllocate
  102.  
  103.             allocateOpMemory 3
  104.             db      9, 10
  105.             finishOpAllocate
  106.  
  107.             allocateOpMemory 4
  108.             db      11
  109.             finishOpAllocate
  110.  
  111.             allocateOpMemory 253
  112.             db      12, 13, 14, 15, "this goes over into page $8100..81FF"
  113.             finishOpAllocate
  114.  
  115.             allocateOpMemory 255
  116.             db      255, 255, 255, 255, 255     ; another going into second half
  117.             finishOpAllocate
  118.  
  119.             lua allpass
  120.                 setOrgAfterLastAllocated()
  121.             endlua
  122.  
  123.         ;-----------------------------------------------------------------
  124.         ; store result as binary blob for simple verification in hexa editor
  125.             align   512, 0              ; fill also last page to full 512B first
  126.             savebin "lua_build_jp_table.bin", opJpTab, $ - opJpTab
  127.