So why not make it so you can control the little turtle guy with the keyboard?

The ultimate goal would be to program a game in mame with lua.

[Linked Image from i.imgur.com]

This lua script assumes that you've already got the vram loaded with the dump from the smartlogo program, and you've initialized the TMS9918 registers:

Code
function heading_to_spritenumber(heading)
  local heading24
  -- reverse the heading direction and rotate by 90 degrees and scale to 0..23
  heading24 = ((-1*(heading - 90)) % 360) *(24/360)
  return  math.floor(36+heading24)*4  -- turtles run from 36 to 59 (24 different sprites patterns)
end

myspriteattributes = {}

for i=0,31 do
  myspriteattributes[i]={ }
end

for i = 0,31 do
  myspriteattributes[i][0] = 128
  myspriteattributes[i][1] = 96
  myspriteattributes[i][2] = (i+1)*4  -- pattern  (since we are doing 16x16 sprites must be multiples of 4
  myspriteattributes[i][3] = (i % 14) + 2   --color
  myspriteattributes[i][4] = .5   --speed
  myspriteattributes[i][5] = i * (360/32) -- heading
  spritespeed = 1
  myspriteattributes[i]["xvel"] = math.cos(myspriteattributes[i][5]*(2*math.pi/360))
  myspriteattributes[i]["yvel"] = math.sin(myspriteattributes[i][5]*(2*math.pi/360))
end


function setspritetable(tablepos,sprite,x,y,pattern,color)
bytepos=tablepos+sprite*4
if y>=0 then mem:write_i8(bytepos,y) end -- vertical first
if x>=0 then mem:write_i8(bytepos+1,x)  end
if pattern>=0 then mem:write_i8(bytepos+2,pattern) end
if color >=0 then mem:write_i8(bytepos+3,color) end
end


function read_keys()
inp = manager:machine():input()
thrust_value = 0.05

if inp:code_pressed(inp:code_from_token("KEYCODE_UP")) then 
myspriteattributes[0]["xvel"] = myspriteattributes[0]["xvel"] + math.cos(myspriteattributes[0][5]*(2*math.pi/360)) * thrust_value
myspriteattributes[0]["yvel"] = myspriteattributes[0]["yvel"] - math.sin(myspriteattributes[0][5]*(2*math.pi/360)) * thrust_value
end

if inp:code_pressed(inp:code_from_token("KEYCODE_DOWN")) then 
myspriteattributes[0]["xvel"] = myspriteattributes[0]["xvel"] * 0.9
myspriteattributes[0]["yvel"] = myspriteattributes[0]["yvel"] * 0.9
end


if inp:code_pressed(inp:code_from_token("KEYCODE_LEFT")) then myspriteattributes[0][5] = myspriteattributes[0][5] + 2 end
if inp:code_pressed(inp:code_from_token("KEYCODE_RIGHT")) then myspriteattributes[0][5] = myspriteattributes[0][5] - 2 end

myspriteattributes[0][2] = heading_to_spritenumber(myspriteattributes[0][5])

if inp:code_pressed(inp:code_from_token("KEYCODE_C")) then setotherspritepattern(128) end
if inp:code_pressed(inp:code_from_token("KEYCODE_D")) then setotherspritepattern(0) end

end




function update_sprites()

for i=0,31 do
  myspriteattributes[i][0] =   myspriteattributes[i][0] + myspriteattributes[i]["xvel"]
  myspriteattributes[i][1] =   myspriteattributes[i][1] + myspriteattributes[i]["yvel"]

if myspriteattributes[i][0] < 0 then  myspriteattributes[i][0] = 256  end
 if myspriteattributes[i][0] > 256 then myspriteattributes[i][0] = 0 end

if myspriteattributes[i][1] < 0 then myspriteattributes[i][1] = 192 end
 if myspriteattributes[i][1] > 192 then myspriteattributes[i][1] = 0 end

  setspritetable(0x1f80,i,math.floor(myspriteattributes[i][0]),
              math.floor(myspriteattributes[i][1]),myspriteattributes[i][2],myspriteattributes[i][3])
end

end




dispatch_list = { }

function frame_dispatcher()
for index,my_func in pairs(dispatch_list) do  my_func() end
end

function dispatch_list_remove(a_func)
for my_index,my_func in pairs(dispatch_list) do if my_func == a_func then table.remove(dispatch_list,my_index) end end
end

function print_dispatch_list() for my_index,my_func in pairs(dispatch_list) do print(my_func) end end

function cld()
dispatch_list = {}
end

print ("cld() installed type cld() and hit enter to stop dispatch")
print()

table.insert(dispatch_list,update_sprites)
table.insert(dispatch_list,read_keys)

print("table.insert(dispatch_list,update_sprites)")
print("table.insert(dispatch_list,read_keys)")
print()
print(" you will need to manually register frame since I don't want to have it done every time I run the lua file")
print()
print(" so copy and paste the following line on the console")
print()
print("emu.register_frame(frame_dispatcher)")
--print("   equivalent to: emu.register_frame(read_keys)")
print()
print("C key to clear the other sprites")
print("D key to bring the other sprites back")
print("UP key for thrust   DOWN key to slow down")
print("LEFT RIGHT to turn")

function setotherspritepattern(spritepattern)
  for i=1,31 do myspriteattributes[i][2]=spritepattern end
end