; printf header
%pragma once
%include "memory"
bits 16

%define Debug pfln
; %define Debug void
; Set :: int8 *dst -> int8 ch -> int16 n -> void
%define set(x,y,z)  _set x,y,z

%macro void 0+
    nop
%endmacro

%macro _set 3
  mov ax,%3
  push ax
  mov ax,%2
  push ax
  mov ax,%1
  push ax
  call Set
  add sp,0x0006
%endmacro

%macro breakpoint 0
   %%loop:
      nop
      jmp %%loop
%endmacro

; %macro ctxidt 0
;     %push idt
; %endmacro

; pf :: int8 *fmt -> ... -> void
%macro pf 1
    jmp %%begin
 %%fmtstr:
    db %1
    db 0x00

 %%begin:
        mov ax,%%fmtstr
        push ax
        call Printf
        add sp,0x02
%endmacro

%macro pf 2
    ; %ifctx idt
    ;  chseg(es,0x20)
    ;  chseg(ds,0x20)
    ; %endif

    mov ax,%2
    push ax
    pf %1
    add sp,0x02

    ; %ifctx idt
    ;  sret(ds)
    ;  sret(es)
    ; %endif
%endmacro

%macro pf 3
    mov ax,%3
    push ax
    mov ax,%2
    push ax
    pf %1
    add sp,0x04
%endmacro

%macro pf 4
    mov ax,%4
    push ax
    mov ax,%3
    push ax
    mov ax,%2
    push ax
    pf %1
    add sp,0x06
%endmacro

%macro pf 5
    mov ax,%5
    push ax
    mov ax,%4
    push ax
    mov ax,%3
    push ax
    mov ax,%2
    push ax
    pf %1
    add sp,0x06
%endmacro

%macro pfln 0
    pf {0x0a,0x0d}
%endmacro

%macro pfln 1+
    pf %1
    pfln
%endmacro

%ifdef psasm
    global Putcharrm
    global Putcharpm
    global Move
    global Set
    global Length
    global Printf
    global Putstring
%else
   extern Putcharrm
   extern Putcharpm
   extern Move
   extern Set
   extern Length
   extern Printf
   extern Putstring
%endif
%define Putchar     Putcharpm
