WWW server in a PIC16F84


pushstate
nowarn res
.lall

W       =           0
F       =           1

Def0 macro name, opcode
    name macro
        dw opcode
    endm
endm
Def1 macro name, opcode
    name macro f:REQ
        dw (opcode) or ((f) and 0ffh)
    endm
endm
Def2 macro name, opcode
    name macro adr:REQ,d:REQ
        dw (opcode) or ((adr) and 7Fh) or (((d) and 1) shl 7)
    endm
endm
Def2o macro name, opcode
    name macro adr:REQ,bit:=<0>
        dw (opcode) or ((adr) and 3FFh) or (((bit) and 7) shl 7)
    endm
endm


Def0  NOP,   0000h
Def0  RETURN,0008h
Def0  RETFIE,0009h
Def0  OPTION,0062h
Def0  SLEEP, 0063h
Def0  CLRWDT,0064h
Def1  TRIS,  0060h

Def1  MOVWF, 0080h
Def1  CLRW,  0100h
Def1  CLRF,  0180h

Def2  SUBWF, 0200h
Def2  DECF,  0300h
Def2  IORWF, 0400h
Def2  ANDWF, 0500h
Def2  XORWF, 0600h
Def2  ADDWF, 0700h
Def2  MOVF,  0800h
Def2  COMF,  0900h
Def2  INCF,  0A00h
Def2  DECFSZ,0B00h
Def2  RRF,   0C00h
Def2  RLF,   0D00h
Def2  SWAPF, 0E00h
Def2  INCFSZ,0F00h

Def2o BCF,   1000h
Def2o BSF,   1400h
Def2o BTFSC, 1800h
Def2o BTFSS, 1C00h

Def1  MOVLW, 3000h
Def1  RETLW, 3400h
Def1  IORLW, 3800h
Def1  ANDLW, 3900h
Def1  XORLW, 3A00h
Def1  SUBLW, 3C00h
Def1  ADDLW, 3E00h

CALL  macro k
  dw  2000h or ((k - __zero) shr 1)
endm

JUMP  macro k
  dw  2800h or ((k - __zero) shr 1)
endm

.ORG  macro k
  org ((k) shr 1)
endm

.DB   macro b:vararg
IRP _,<b>
  retlw _
endm
endm


INDR    =           0000h
RTCC    =           0001h
PCL     =           0002h
STATUS  =           0003h
FSR     =           0004h
PORTA   =           0005h
PORTB   =           0006h
EEDATA  =           0008h
EEADR   =           0009h
PCLATH  =           000Ah
INTCON  =           000Bh


IRP _I_,<  0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19>
SRAM&_I_  =   (000Ch+_I_)
ENDM
IRP _I_,< 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39>
SRAM&_I_  =   (000Ch+_I_)
ENDM
IRP _I_,< 40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59>
SRAM&_I_  =   (000Ch+_I_)
ENDM
IRP _I_,< 60,61,62,63,64,65,66,67>
SRAM&_I_  =   (000Ch+_I_)
ENDM


; second bank

OPTIO   =           0001h
TRISA   =           0005h
TRISB   =           0006h
EECON1  =           0008h
EECON2  =           0009h


DefBit macro name, address, bit
    name =  (address and 7Fh) or ((bit and 7) shl 7)
endm

DefBit  GIE,        INTCON,7
DefBit  T0IE,       INTCON,5
DefBit  INTE,       INTCON,4
DefBit  RBIE,       INTCON,3
DefBit  T0IF,       INTCON,2
DefBit  INTF,       INTCON,1
DefBit  RBIF,       INTCON,0

DefBit  NOT_RBPU,   OPTIO,7
DefBit  INTEDG,     OPTIO,6
DefBit  RTS,        OPTIO,5
DefBit  T0SS,       OPTIO,5
DefBit  RTE,        OPTIO,4
DefBit  T0SE,       OPTIO,4
DefBit  PSA,        OPTIO,3
DefBit  PS2,        OPTIO,2
DefBit  PS1,        OPTIO,1
DefBit  PS0,        OPTIO,0

DefBit  IRP_,       STATUS,7
DefBit  RP1,        STATUS,6
DefBit  RP0,        STATUS,5
DefBit  NOT_TO,     STATUS,4
DefBit  NOT_PD,     STATUS,3
DefBit  Z,          STATUS,2
DefBit  DC,         STATUS,1
DefBit  C,          STATUS,0

SKIPNZ macro    ;; skip if Z==0
  btfsc     Z
endm
SKIPZ  macro    ;; skip if Z==1
  btfss     Z
endm
SKIPNC macro    ;; skip if C==0
  btfsc     C
endm
SKIPC  macro    ;; skip if C==1
  btfss     C
endm
SKIP0 macro bit ;; skip if bit==0
  btfsc     bit
endm
SKIP1 macro bit ;; skip if bit==1
  btfss     bit
endm

BRAKE  macro
local l1
  jump      l1
l1:
endm

JNZ   macro k   ;; jump if Z==0
  skipz
  jump      k
endm
JZ    macro k   ;; jump if Z==1
  skipnz
  jump      k
endm
JNC   macro k   ;; jump if C==0
  skipc
  jump      k
endm
JC    macro k   ;; jump if C==1
  skipnc
  jump      k
endm
J0    macro bit,k   ;; jump if bit==0
  btfss     bit
  jump      k
endm
J1    macro bit,k   ;; jump if bit==1
  btfsc     bit
  jump      k
endm

; max delay is 1027
DELAY MACRO dtime:req   ; fuck???
local l1
    if (dtime) eq 0
elseif (dtime) eq 1
        nop
elseif (dtime) eq 2
        brake
elseif (dtime) eq 3
        brake
        nop
elseif (dtime) eq 4
        brake
        brake
elseif (dtime) eq 5
        brake
        brake
        nop
elseif (dtime) eq 6
        brake
        brake
        brake
elseif (dtime) eq 7
        brake
        brake
        brake
        nop
elseif (dtime) eq 8
        brake
        brake
        brake
        brake
elseif (dtime) eq 9
        brake
        brake
        brake
        brake
        nop
elseif ((dtime)/4) le 256
;; n==FF dtime=4
;; n==FE dtime=8
;; n==00 dtime=1024
        movlw       100h-((dtime)/4)    ;; 1
l1:     addlw       -1                  ;; 1
        skipz                           ;; 1/2
        jump        l1                  ;; 2
if ((dtime)mod 4) eq 1
        nop
elseif ((dtime)mod 4) eq 2
        brake
elseif ((dtime)mod 4) eq 3
        brake
        nop
endif
else
error
endif
ENDM

DELAY_OLD MACRO dtime:req
local l1,l2,l3,l4
if dtime eq 0
elseif dtime eq 1
        nop
elseif dtime eq 2
        jump        l1
l1:
elseif dtime eq 3
        jump        l1
l1:     nop
elseif dtime eq 4
        jump        l1
l1:     jump        l2
l2:
elseif dtime eq 5
        jump        l1
l1:     jump        l2
l2:     nop
elseif dtime eq 6
        jump        l1
l1:     jump        l2
l2:     jump        l3
l3:
elseif dtime eq 7
        jump        l1
l1:     jump        l2
l2:     jump        l3
l3:     nop
elseif dtime eq 8
        jump        l1
l1:     jump        l2
l2:     jump        l3
l3:     jump        l4
l4:
elseif dtime eq 9
        jump        l1
l1:     jump        l2
l2:     jump        l3
l3:     jump        l4
l4:     nop
elseif ((dtime-1)/3) le 255
        movlw       (dtime-1)/3         ;; 1         \
        movwf       TEMP_DELAY          ;; 1         |
l1:     decfsz      TEMP_DELAY,f        ;; n*1 or 2   > (4+n*3)
        jump        l1                  ;; n*2       /
if ((dtime-1)mod 3) eq 1
        nop                             ;; 1  (6+n*3)
elseif ((dtime-1)mod 3) eq 2
        jump        l2                  ;; 2  (5+n*3)
l2:
endif
else
error
endif
ENDM


popstate

.model tiny
.code
__zero label byte