Here's a small improvement:

Using emu.item(manager:machine().devices[":ram"].items["0/m_pointer"]):read() will always get main memory so the scrolling is not as strange while the softswitches are getting hit.

Also we do inverse characters by flipping the foreground/background color so you can see the cursor.

Code

-- 80 column text overlay for Apple IIe

function calc(line,page) page=page or 0 return line % 8 * 1024 + math.floor(line / 64) * 40 + math.floor((line%64) / 8) * 128 + 8192* (page + 1) end

function iskeypressed(keycode) 
inp = manager:machine():input() return inp:code_pressed(inp:code_from_token(keycode))
end

function striphi(a) if type(a)=="number" then a=string.char(a) end local b="" for i=1,#a do b=b..string.char(string.byte(a,i)&0x7f) end return b end

mem=manager:machine().devices[":maincpu"].spaces["program"]              -- gets main or aux, depending on softswitches
aux=emu.item(manager:machine().devices[":aux:ext80"].items["0/m_ram"])   -- gets aux memory
scr=manager:machine().screens[":screen"]
main=emu.item(manager:machine().devices[":ram"].items["0/m_pointer"])  -- always gets main memory

function widthpct(c) if c==nil or c==string.char(0) then return 0 end return ((manager:ui():get_string_width(c,1.0)/manager:ui():get_string_width("A",1.0))) end
function widthoffset(c) if c==nil or c==string.char(0) then return 0 end return 3.5*(1-widthpct(c)) end


function between(a,b,c) return (a >= b) and (a <= c) end

function applecharfixinverse(c)
  if type(c)=="string" then c=string.byte(c) end
  if c == 0 then return string.char(32),false end -- looks terrible with @ signs at bootup
  if between(c,0,31) then return string.char(c+64),true
  elseif between (c,32,127) then return string.char(c), true
  elseif between (c,128,159) then return string.char(c-128+64), false
  elseif between (c,160,255) then return string.char(c-128), false
  else return 0, false
  end
end

drawoverlay=true
keydelay=0

function print_80col()
for y=0,23 do for x=0,39 do offset=calc(y*8)-8192+x 
c1=striphi(aux:read(1024+offset)) c0=striphi(mem:read_u8(1024+offset)) 
io.write(c1..c0) end print() end
end

function draw_80col()
if keydelay > 0 then keydelay = keydelay - 1 end
if drawoverlay then 
scr:draw_box(0,0,559,279,0xff000000,0xffffffff)for y=0,23 do for x=0,39 do offset=calc(y*8)-8192+x 
c1=aux:read(1024+offset)
c1,c1invert=applecharfixinverse(c1)
c0=main:read(1024+offset)
c0,c0invert=applecharfixinverse(c0)
fgcolor,bgcolor=0xffffffff,0xff000000
if c1invert then fgcolor,bgcolor=bgcolor,fgcolor end
scr:draw_text((x*2+0)*7+widthoffset(c1),y*8,c1,fgcolor,bgcolor) 
fgcolor,bgcolor=0xffffffff,0xff000000
if c0invert then fgcolor,bgcolor=bgcolor,fgcolor end
scr:draw_text((x*2+1)*7+widthoffset(c0),y*8,c0,fgcolor,bgcolor) end end
end
if iskeypressed("KEYCODE_LSHIFT") and keydelay==0 then drawoverlay=not drawoverlay keydelay=20 end
end

draw80dispatchlist={draw_80col}

function draw80dispatch() for i,j in pairs(draw80dispatchlist) do j() end end

function drawon() draw80dispatchlist={draw_80col} end
function drawoff() draw80dispatchlist={} end

if not alreadyregistereddraw80 then
  emu.register_frame_done(draw80dispatch)
  alreadyregistereddraw80=true
end



oops - I added a check for the 0 character since the aux card gets all 0's on bootup and it makes it hard to read.

Last edited by Golden Child; 03/03/19 12:32 PM.