'*****************************************************************************
'* INT 10H AH=0Eh (CGA BIOS) TELETYPE-LIKE CHARS OUTPUT
'* Result: AL - char
'*         BL - foreground color (graph mode only)
'*         BH - video page
'*****************************************************************************

//DEBUG.ON 'uncomment to enable debug messages (can be slow for hi-freq events)

use module "Tools" 'cga tools

protected function Exec as boolean

//?? "INT10 OE AH=";Hex(cpu.AH,2);" AL=";Hex(cpu.AL,2);"(";Chr(cpu.AL);")"
//dbg.Break

//params
dim Char as byte=cpu.AL 'char
dim Color as byte=cpu.BL 'graph char fgcolor
dim Page as byte=cpu.BH : if Page>7 then exit(false) 'video page
dim Col as byte=mem.Byte(0x450+Page*2) 'cursor column in page
dim Row as byte=mem.Byte(0x451+Page*2) 'cursor row in page
dim Cols as word=mem.Word(0x44A), Rows as word=25 'always on CGA
dim NewRow as word
dim mode as byte=mem.Byte(0x449) 'video mode

//analize and output
select case Char 
  case 0xD //start row
    NewRow=Row : Col=0
  case 0xA //next row
    NewRow=Row+1 : ?? " "
  case 0x7 //Beep
    NewRow=Row
  case 0x8 //Backspace
    NewRow=Row : if Col>0 then Col=Col-1
  case else //Char 
    if  mode<=3 then
//       ? mode;"<=3"
       result=WriteTextChar(Page,Row,Col,Char,-1,1)
    else
//       ? mode;">3"
       result=WriteGraphChar(Page,Row,Col,Char,Color,0,1)
    end if
    ?? Chr(Char);
    if not(result) then exit(false) else result=false
    Col=Col+1 : if Col>=Cols then : NewRow=Row+1 : ?? " " : Col=0 
    else : NewRow=Row : end if
end select


//scroll page if needed
if NewRow>=Rows then

 if IsTextMode then
    result=ScrollTextRect(Page,0,0,Cols-1,Rows-1,-1,0,-1)
 else 
    result=ScrollGraphRect(Page,0,0,Cols-1,Rows-1,-1,0,0)
 end if
 if not(result) then exit(false)
 Row=Rows-1

else : Row=NewRow : end if

//set new cursor position
result=SetCursorPos(Page,Row,Col) 

//success
result=true : end