%from IMP %include Mcode, Formats, Sysspecs, Depio

%externalroutine Flush TT Output %alias "3L___flush_tt_output"(%integer SS)
   %record(SCB Fm) %name S == Record(SS)
   %integer A, Size
   %label L1

   Size    = S_Next - S_ThisB_Base     {bytes to flush}
   %return %if Size = 0
   S_Next  = S_ThisB_Base              {tidy up the SCB}
   S_Pseudo Next = S_Next
   S_Limit = S_Next + S_ThisB_Size-1

   A = S_ThisB_Base

   *LDR   _ R9, Size           {bytes to transfer}
   *LDR   _ R5, A              {address of first byte}
L1:*LDRB  _ R0, [R5], #1       {pick up the next byte}
   *CMPS  _ R0, #10            {newline?}
   *SWINE _ Brasil WriteC      {no, treat normally}
   *SWIEQ _ Brasil NewLine     {yes, treat specially}
   *SUBS  _ R9, R9, #1
   *BGT   _ L1
%end

%external %routine TT Output Full %alias "3L___tt_output_full"
   {R1 = addr(SCB)}
   {R0 = Char}
   %integer Char, H, A, Size, S
   *STR _ R0, Char
   *STR _ R1, S
   Flush TT Output(S)
   *LDR _ R0, Char
   *LDR _ R2, S
   *LDR _ R3, [R2]      {buffer pointer}
   *STRB_ R0, [R3], #1
   *STR _ R3, [R2]
%end

%externalroutine Close TT Output %alias "3L___close_tt_output"(%integer SS, Ab)
   Flush TT Output(SS)
   Delete TTY(Record(SS))
%end
