I wanted to see if I could rip atari 2600 graphics from the game Stampede, so I wrote a little lua script to dump the ram from the 2600.

I didn't know how much to grab, so I went for 4k. I now know that Stampede is a 2k cartridge.

Code
mem=manager:machine().devices[":maincpu"].spaces["program"]

-- read ram and save to a file

outfile = assert(io.open("2600_dump_file", "wb"))

for i=0x1000,0x2000-1 do
  outfile:write(string.char(mem:read_u8(i)))
end

assert(outfile:close())

and once you have the 2600_dump_file saved, you can run this script on the apple2e driver with -sl4 arcbd which will make the following display:

The horse animates and you can move it left right/up and down.


I always wondered why the legs of the horse had an interlaced effect. That's because of the HMOVEs so the legs can split and display wider than the 8 pixels in a byte.

[Linked Image from i.imgur.com]

Code
-- ==============================================================
-- read datablock from a file and close it

infile = assert(io.open("2600_dump_file", "rb"))

datablock = infile:read("*all")    

assert(infile:close())

-- ====================================================================

emu.item(manager:machine().devices[":sl4:arcbd:arcbd_tms"].items["0/m_Regs[0]"]):write(0,2)
emu.item(manager:machine().devices[":sl4:arcbd:arcbd_tms"].items["0/m_Regs[1]"]):write(0,0xc2)
emu.item(manager:machine().devices[":sl4:arcbd:arcbd_tms"].items["0/m_Regs[1]"]):write(0,0xc3) -- dbl size
emu.item(manager:machine().devices[":sl4:arcbd:arcbd_tms"].items["0/m_pattern"]):write(0,0)
emu.item(manager:machine().devices[":sl4:arcbd:arcbd_tms"].items["0/m_colour"]):write(0,0x2000)
emu.item(manager:machine().devices[":sl4:arcbd:arcbd_tms"].items["0/m_nametbl"]):write(0,0x3800)
emu.item(manager:machine().devices[":sl4:arcbd:arcbd_tms"].items["0/m_mode"]):write(0,2)
emu.item(manager:machine().devices[":sl4:arcbd:arcbd_tms"].items["0/m_spritepattern"]):write(0,0x1800)
emu.item(manager:machine().devices[":sl4:arcbd:arcbd_tms"].items["0/m_spriteattribute"]):write(0,0x1f80)
--set background to 4
emu.item(manager:machine().devices[":sl4:arcbd:arcbd_tms"].items["0/m_Regs[7]"]):write(0,0xf4)

mem=manager:machine().devices[":sl4:arcbd:arcbd_tms"].spaces["vram"]

function plotpixel(screenaddr,x,y)
local byteaddr = screenaddr + math.floor(y/8) * 256 + math.floor(y % 8) + math.floor(x/8) * 8
if x>=0 and x <=255 and y>=0 and y <=191 then
mem:write_u8(byteaddr,mem:read_u8(byteaddr)| (1 << (7-(x%8))))
end
end


function plotcolor(screenaddr,x,y,colorfg)
local byteaddr = screenaddr + math.floor(y/8) * 256 + math.floor(y % 8) + math.floor(x/8) * 8
if x>=0 and x<=255 and y>=0 and y<=191 then
--foreground single color
mem:write_u8(byteaddr,(mem:read_u8(byteaddr)& 0x0) | (math.floor(colorfg) << (4)) )
end
end

function deltasign(x)
if x<0 then return -1
elseif x==0 then return 0
elseif x>0 then return 1
else return 0
end
end

function linedraw(x1,y1,x2,y2,colorfg)
-- using basic code converted to lua from:
-- https://rosettacode.org/wiki/Bitmap/Bresenham's_line_algorithm

 x1 = math.floor(x1);x2 = math.floor(x2);y1 = math.floor(y1);y2 = math.floor(y2)
 dx = x2-x1; dy = y2-y1
 sx = deltasign(dx); sy = deltasign(dy)
 local x = x1; local y = y1
 local adx = math.abs(dx)
 local ady = math.abs(dy)
 local er,e2

if adx > ady then er =  adx 
else er = - ady
end 

er = math.floor((er / 2)+0.5)  -- INT rounding because math.floor rounding doesn't work for 1 pixel lines

while 1 do
  plotpixel(0,x,y)
  plotcolor(8192,x,y,colorfg)
  if x == x2 and y == y2 then return end
  e2 = er
  if e2 > -adx then er = er - ady; x = x + sx  end
  if e2 <  ady then er = er + adx; y = y + sy  end
  if x<0 or x > 255 or y < 0 or y > 191 then print("hit edge",x1,y1,x2,y2) return end
end  -- while
end -- function

function drawbox(x1,y1,x2,y2,col)
 linedraw(x1,y1,x2,y1,col)
 linedraw(x2,y1,x2,y2,col)
 linedraw(x2,y2,x1,y2,col)
 linedraw(x1,y2,x1,y1,col)
end


function clearscreentms()
tms= manager:machine().devices[":sl4:arcbd:arcbd_tms"]
mem = tms.spaces["vram"]
local addr
for addr = 0,6143 do mem:write_u8(addr,0) end
end

function initpatterntms()
local pat
for pat = 0,768-1 do
   mem:write_u8(pat+0x3800,pat % 256)
end
end

function initcolortms(fg,bg)
for addr = 0,6143 do mem:write_u8(addr+8192,bg|(fg<<4)) end end


function clstms()
clearscreentms()
initpatterntms()
initcolortms(15,4)
end

clstms()

function reversebits(x)
local outbyte = 0
for bit=0,7 do
  if(x & (1<<bit))~=0 then outbyte = outbyte | (1<<(7-bit)) end
end
return outbyte
end

function draw2600intoscreendirect()
for i=0,4096-1 do
  mem:write_u8(i,reversebits(string.byte(datablock,(i+1))))  -- datablock index starts at 1
end
end

clstms()

function draw2600intoscreen()
xpos = 0; ypos = 0
for i = 4096-1,4096-2048-1,-1 do
  for bit=0,7 do
    if (reversebits(string.byte(datablock,(i+1))) & (1<<bit))~=0 then linedraw(xpos+bit,ypos,xpos+bit,ypos,7) end
  end
  ypos = ypos + 1
  if ypos >= 128+16 then ypos = 0; xpos = xpos + 16; end
end
end

clstms()
draw2600intoscreen()

function stampededogie(p8d,p8f)
--linedraw(xpos,ypos,xpos,ypos,1)
for offset = 0xf,1,-1 do
grp1 =p8f+offset
hmove=p8d+offset-1
  for bit=0,7 do
    if (reversebits(string.byte(datablock,(grp1+1))) & (1<<bit))~=0 then linedraw(xpos+bit,ypos,xpos+bit,ypos,7) end
  end
  hmoveval = (string.byte(datablock,(hmove+1))>>4)
  if hmoveval >= 8 then hmoveval = hmoveval - 16 end
  xpos = xpos - hmoveval
  ypos = ypos + 1
  if ypos >= 128+64 then ypos = 128+32; xpos = xpos + 16; end
end
end

xpos = 32;       ypos = 128+48; stampededogie(0x6c9,0x67d);
xpos = 32+1*16;  ypos = 128+48; stampededogie(0x6c9,0x66e);
xpos = 32+2*16+6;ypos = 128+48; stampededogie(0x6ac,0x68b)

function stampede_horse(p8b,p8d,p8f) 
--p8b is ptr to hmp0, p8d is ptr to nusiz0, p8f is ptr to grp0
xpos = xpos + 32
ypos = 128+24
for offset=0x19,1,-1 do
grp0 =p8f+offset
hmove=p8b+offset-1  -- hmove happens to "next line"
nusiz=p8d+offset
-- follows the atari 2600 stampede code which ORs these 2 together
nusiz=string.byte(datablock,(nusiz+1)) | (string.byte(datablock,(hmove+1+1))&5)
nusiz=nusiz & 0xd
  if (nusiz & 7) == 5 then dbl=2 else dbl=1 end
  for bit=0,7 do
    if (reversebits(string.byte(datablock,(grp0+1))) & (1<<bit))~=0 then 
          linedraw(xpos+bit*dbl,ypos,xpos+(bit+1)*dbl-1,ypos,7+dbl)
    end
  end
  hmovedelta = (string.byte(datablock,(hmove+1))>>4)
  if hmovedelta >= 8 then hmovedelta = hmovedelta - 16 end
  xpos = xpos - hmovedelta
  ypos = ypos + 1
  if ypos >= 128+64 then ypos = 128+24; xpos = xpos + 32; end
end
end

xpos=64;      stampede_horse(0x74d,0x6c7,0x71a); 
xpos=64+1*32; stampede_horse(0x766,0x6c7,0x700); 
xpos=64+2*32; stampede_horse(0x74d,0x6c7,0x700); 
xpos=64+3*32; stampede_horse(0x77f,0x6c3,0x734);

function drawblock(addr,offset1,offset2)
ypos0 = ypos
for i=addr+offset1,addr+offset2,-1 do
  for bit=0,7 do
    if (reversebits(string.byte(datablock,(i+1))) & (1<<bit))~=0 then 
       linedraw(xpos+bit,ypos,xpos+bit,ypos,10)
    end
  end    
  ypos = ypos + 1
end
xpos = xpos + 8
ypos = ypos0
end

function stampede_activision()
drawblock(0x668,6,0) -- act
drawblock(0x660,6,0) -- iv
drawblock(0x658,6,0) -- isi
drawblock(0x650,6,0) -- on
end

xpos = 128; ypos= 128+56
stampede_activision()

function stampede_skull()
drawblock(0x69b,9,1)
end

xpos = 64;ypos = 128+20
stampede_skull()

function stampede_0123456789()
for num = 0,9 do
drawblock(0x600+num*8,6,0)
end
end

xpos = 0;  ypos = 128+30; stampede_0123456789()

function xytoaddress(x,y)
--print(x,y,math.floor(y/8)*256+(y%8)+math.floor(x/8)*8)
x = math.floor(x)
y = math.floor(y)
return math.floor(y/8)*256+(y%8)+math.floor(x/8)*8
end

for box=0,3 do drawbox(80+32*box,150,80+32*(box+1)-1,150+31,9) end

-- 4 different 16x16 blocks

bytepos=0x1800; 
for box = 0,3 do
for cellx = 0,1 do for celly = 0,1 do for j = 0,1 do for i=0,15 do mem:write_u8(bytepos,mem:read_u8(xytoaddress(80+8*j+16*cellx+box*32,150+i+16*celly))); bytepos = bytepos + 1; end end end end end


spriteatt = 0x1f80

horsex = 25
horsey = 25

scale = 2

function setsprite(sprnum,sprimgnum)
for celly = 0,1 do
for cellx = 0,1 do
mem:write_u8(spriteatt + cellx*2*4 + celly*4 + 0, horsey+16*celly*scale)
mem:write_u8(spriteatt + cellx*2*4 + celly*4 + 1, horsex+16*cellx*scale)
mem:write_u8(spriteatt + cellx*2*4 + celly*4 + 2,(cellx * 2 + celly)*4+16*sprimgnum)
mem:write_u8(spriteatt + cellx*2*4 + celly*4 + 3, 11)
end
end
end


setsprite(0,0)


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



if already_registered_frame_dispatcher == nil then
   emu.register_frame(frame_dispatcher);
   already_registered_frame_dispatcher = 1
end

function cld()
dispatch_list = {}
end


delaycounter1 = 0
spriteimageseq = {0,1,2,1}
spriteimageseqnum = 1


function every_frame()
--  everydelay(1,delaycounter1)
  
  delaycounter1 = (delaycounter1+1)%5
  if delaycounter1 == 0 then 
           spriteimageseqnum = (((spriteimageseqnum + 1)-1) % #spriteimageseq)+1
  end

  inp = manager:machine():input()
  if inp:code_pressed(inp:code_from_token("KEYCODE_UP")) then 
     horsey = horsey - 2
   end

  if inp:code_pressed(inp:code_from_token("KEYCODE_DOWN")) then 
     horsey = horsey + 2
  end
      
  if inp:code_pressed(inp:code_from_token("KEYCODE_RIGHT")) then 
    horsex = horsex + 2
  end
  if inp:code_pressed(inp:code_from_token("KEYCODE_LEFT")) then 
    horsex = horsex - 2
  end


  setsprite(0,spriteimageseq[spriteimageseqnum])
  

end

table.insert(dispatch_list,every_frame)