Remove an unneeded comment.

Remove an unneeded comment.

// Pin control drivers

staload "Bit.sats"

// Target MCU
#define MCU_RX110
#define MCU_Package_Pins 64

datasort IOPort =
 | Port0 of () | Port1 of () | Port2 of () | Port3 of ()
 | Port4 of () | Port5 of () | Port6 of () | Port7 of ()
 | Port8 of () | Port9 of () | PortA of () | PortB of ()
 | PortC of () | PortD of () | PortE of () | PortF of ()
 | PortG of () | PortH of () | PortI of () | PortJ of ()

stacst IOPort2int (p:IOPort):int
praxi ioport_eq_int ():[
    IOPort2int Port0 == 0 && IOPort2int Port1 == 1 &&
    IOPort2int Port2 == 2 && IOPort2int Port3 == 3 &&
    IOPort2int Port4 == 4 && IOPort2int Port5 == 5 &&
    IOPort2int Port6 == 6 && IOPort2int Port7 == 7 &&
    IOPort2int Port8 == 8 && IOPort2int Port9 == 9 &&
    IOPort2int PortA == 10 && IOPort2int PortB == 11 &&
    IOPort2int PortC == 12 && IOPort2int PortD == 13 &&
    IOPort2int PortE == 14 && IOPort2int PortF == 15 &&
    IOPort2int PortG == 16 && IOPort2int PortH == 17 &&
    IOPort2int PortI == 18 && IOPort2int PortJ == 19
  ] void

typedef ioport_t (p:IOPort) = int (IOPort2int p)

datasort Pin = Pin of (IOPort,int)

typedef pin_t (p:IOPort,n:int) = @(ioport_t p, uint n)

stadef P0 (n:int):Pin = Pin (Port0,n)
stadef P1 (n:int):Pin = Pin (Port1,n)
stadef P2 (n:int):Pin = Pin (Port2,n)
stadef P3 (n:int):Pin = Pin (Port3,n)
stadef P4 (n:int):Pin = Pin (Port4,n)
stadef P5 (n:int):Pin = Pin (Port5,n)
stadef P6 (n:int):Pin = Pin (Port6,n)
stadef P7 (n:int):Pin = Pin (Port7,n)
stadef P8 (n:int):Pin = Pin (Port8,n)
stadef P9 (n:int):Pin = Pin (Port9,n)
stadef PA (n:int):Pin = Pin (PortA,n)
stadef PB (n:int):Pin = Pin (PortB,n)
stadef PC (n:int):Pin = Pin (PortC,n)
stadef PD (n:int):Pin = Pin (PortD,n)
stadef PE (n:int):Pin = Pin (PortE,n)
stadef PF (n:int):Pin = Pin (PortF,n)
stadef PG (n:int):Pin = Pin (PortG,n)
stadef PH (n:int):Pin = Pin (PortH,n)
stadef PI (n:int):Pin = Pin (PortI,n)
stadef PJ (n:int):Pin = Pin (PortJ,n)

// Special form of fractional (n/2^m only)
datasort bisectional = bisectional of (int,int)

stadef bisect1 = bisectional (1,0)

dataprop EQBISECTIONAL (bisectional, bisectional) = {r:bisectional} EQBISECTIONAL (r, r)
praxi eqbisectional_make {r,s:bisectional | r == s} () : EQBISECTIONAL (r,s)
praxi bisectional_eq_refl {r:bisectional} () : [r == r] void
praxi bisectional_eq_reduce {n,m:int | n <= INTMAX_HALF} ():
      [bisectional (n,m) == bisectional (n+n,m+1)] void

stacst bisectional_add_b : (bisectional,bisectional,bisectional) -> bool

praxi {n1,n2,m:int} bisectional_add_axi ():
      [bisectional_add_b (bisectional (n1,m),bisectional (n2,m),bisectional (n1+n2,m))] unit_p

praxi bisectional_add_axi_rev
 {n1,n2,n3,m:int | bisectional_add_b (bisectional (n1,m),bisectional (n2,m),bisectional (n3,m))}(): [n1+n2==n3] unit_p

stacst bisectional_half_b : (bisectional,bisectional) -> bool

praxi {n,m:int} bisectional_half_axi ():
      [bisectional_half_b (bisectional (n,m),bisectional (n,m+1))] unit_p

praxi {n,m,l:int} bisectional_half_axi_rev {bisectional_half_b (bisectional (n,m),bisectional (n,l))}(): [m+1==l] unit_p

// PMR

absview PMR_BIT_V (Pin,bit,bisectional)

praxi {p:Pin}{b:bit} divide_pmr_bit_v {r,s:bisectional | bisectional_half_b (r,s)}
      (PMR_BIT_V (p,b,r)) : (PMR_BIT_V (p,b,s),PMR_BIT_V (p,b,s))

praxi {p:Pin}{b:bit} merge_pmr_bit_v {r,s,t:bisectional | bisectional_add_b (r,s,t)}
      (PMR_BIT_V (p,b,r), PMR_BIT_V (p,b,s)):(PMR_BIT_V (p,b,t))

stadef PMR_BIT_IOPORT     = O
stadef PMR_BIT_PERIPHERAL = I

dataview PMR_V (IOPort,bits) =
    {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}
    PMR_V (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) of
      (PMR_BIT_V (Pin (p,7),b7,bisect1), PMR_BIT_V (Pin (p,6),b6,bisect1),
       PMR_BIT_V (Pin (p,5),b5,bisect1), PMR_BIT_V (Pin (p,4),b4,bisect1),
       PMR_BIT_V (Pin (p,3),b3,bisect1), PMR_BIT_V (Pin (p,2),b2,bisect1),
       PMR_BIT_V (Pin (p,1),b1,bisect1), PMR_BIT_V (Pin (p,0),b0,bisect1))

absview PMR_PERMISSION (IOPort,bit_permissions)

viewdef PMR_PERMIT (p:IOPort,bs:bits) =
    [perms:bit_permissions]
    (PMR_PERMISSION (p,perms),BITS_PERMIT_CERTIFICATE (8,perms,bs))

fn {p:IOPort}{bs,cs:bits}
  writePMR (!PMR_V (p,bs) >> PMR_V (p,cs),
            !PMR_PERMIT (p,cs)
           | ioport_t p,bits_uint_t (8,cs))
   :<!wrt> void

fn {p:IOPort}{bs,cs:bits}{b:bit}
  changePMRBit {bn:int | bn < 8}
    (CHANGE_BIT_BITS (8,bs,bn,b,cs),
     !PMR_V (p,bs) >> PMR_V (p,cs),
     !PMR_PERMIT (p,cs)
    | pin_t (p,bn), bit_uint_t b):<!refwrt> void

fn {p:IOPort}{bs:bits}
  readPMR (!PMR_V (p,bs) | p:ioport_t p)
   :<!ref> bits_uint_t (8,bs)

fn pmr2pmr_bits {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}{v:int}
   (PMR_V (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) | bits_uint_t (8,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)))
   :(PMR_BIT_V (Pin (p,7),b7,bisect1),
     PMR_BIT_V (Pin (p,6),b6,bisect1),
     PMR_BIT_V (Pin (p,5),b5,bisect1),
     PMR_BIT_V (Pin (p,4),b4,bisect1),
     PMR_BIT_V (Pin (p,3),b3,bisect1),
     PMR_BIT_V (Pin (p,2),b2,bisect1),
     PMR_BIT_V (Pin (p,1),b1,bisect1),
     PMR_BIT_V (Pin (p,0),b0,bisect1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),0,b0),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),1,b1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),2,b2),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),3,b3),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),4,b4),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),5,b5),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),6,b6),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),7,b7)
    | @(bool (b7 == I),bool (b6 == I),bool (b5 == I),bool (b4 == I),
        bool (b3 == I),bool (b2 == I),bool (b1 == I),bool (b0 == I)))

// The PFS function is postponed.
// Define only PFS_V so that other functions can acquire the state of the initial value
(*
// PWPR

absview PWPR_V (bits)

fn writePWPR {
    pfswe,b0wi,PFSWE,B0WI:bit
    | (pfswe==PFSWE) || (b0wi==O && B0WI==O)
  }(
    !PWPR_V (Bits8 (b0wi,pfswe,O,O,O,O,O,O)) >>
     PWPR_V (Bits8 (B0WI,PFSWE,O,O,O,O,O,O)) |
     bits8int_t (B0WI,PFSWE,O,O,O,O,O,O)
  ):void

fn {pfswe,b0wi:bit}
  readPWPR (!PWPR_V (Bits8 (b0wi,pfswe,O,O,O,O,O,O)))
   :bits8int_t (b0wi,pfswe,O,O,O,O,O,O)
*)

// PFS

absview PFS_V (Pin,bits)
(*
fn {p:IOPort}{pnum:int}
   {b0wi,asel,isel,psel4,psel3,psel2,psel1,psel0:bit}
  writePFS (
    !PWPR_V (Bits8 (O,O,O,O,O,O,I,b0wi)),
    !PFS_V (Pin (p,pnum),Bits8 (asel,isel,O,psel4,psel3,psel2,psel1,psel0)) >>
     PFS_V (Pin (p,pnum),Bits8 (O,O,O,O,O,O,O,O)),
    !PMR_BIT_V (Pin (p,pnum), O)
  | pin_t (p,pnum),
    bits8int_t (O,O,O,O,O,O,O,O)
  )
  :void

fn {p:IOPort}{pnum:int}{asel,isel,psel4,psel3,psel2,psel1,psel0:bit}
  readPFS (
    !PFS_V (Pin (p,pnum),Bits8 (asel,isel,O,psel4,psel3,psel2,psel1,psel0))
  | pin_t (p, pnum)
  )
  :bits8int_t (asel,isel,O,psel4,psel3,psel2,psel1,psel0)
*)

// TODO Create duplicate prohibited view of peripheral devices.
//      Issues view with no device assignment pins first.
//       Propriety of writing / disabling per pins is required.

// PDR

stadef PDR_BIT_WRITABLE  = I
stadef PDR_BIT_READ_ONLY = O

absview PDR_BIT_V (Pin,bit,bisectional)

praxi {p:Pin}{b:bit} divide_pdr_bit_v {r,s:bisectional | bisectional_half_b (r,s)}
      (PDR_BIT_V (p,b,r)) : (PDR_BIT_V (p,b,s),PDR_BIT_V (p,b,s))

praxi {p:Pin}{b:bit} merge_pdr_bit_v {r,s,t:bisectional | bisectional_add_b (r,s,t)}
      (PDR_BIT_V (p,b,r), PDR_BIT_V (p,b,s)):(PDR_BIT_V (p,b,t))

dataview PDR_V (p:IOPort,bits) =
    {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}
    PDR_V (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) of
      (PDR_BIT_V (Pin (p,7),b7,bisect1), PDR_BIT_V (Pin (p,6),b6,bisect1),
       PDR_BIT_V (Pin (p,5),b5,bisect1), PDR_BIT_V (Pin (p,4),b4,bisect1),
       PDR_BIT_V (Pin (p,3),b3,bisect1), PDR_BIT_V (Pin (p,2),b2,bisect1),
       PDR_BIT_V (Pin (p,1),b1,bisect1), PDR_BIT_V (Pin (p,0),b0,bisect1))

absview PDR_PERMISSION (IOPort,bit_permissions)

viewdef PDR_PERMIT (p:IOPort,bs:bits) =
    [perms:bit_permissions]
    (PDR_PERMISSION (p,perms),BITS_PERMIT_CERTIFICATE (8,perms,bs))

fn {p:IOPort}{bs,cs:bits}
  writePDR (!PDR_V (p,bs) >> PDR_V (p,cs),
            !PDR_PERMIT (p,cs)
           | ioport_t p,bits_uint_t (8,cs)):<!wrt> void

fn {p:IOPort}{bs,cs:bits}{b:bit}
  changePDRBit {bn:int | bn < 8}
    (CHANGE_BIT_BITS (8,bs,bn,b,cs),
     !PDR_V (p,bs) >> PDR_V (p,cs),
     !PDR_PERMIT (p,cs)
    | pin_t (p,bn), bit_uint_t b):<!refwrt> void

fn {p:IOPort}{bs:bits}
  readPDR (!PDR_V (p,bs) | ioport_t p)
   :<!ref> bits_uint_t (8,bs)

fn pdr2pdr_bits {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}{v:int}
   (PDR_V (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) | bits_uint_t (8,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)))
   :(PDR_BIT_V (Pin (p,7),b7,bisect1),
     PDR_BIT_V (Pin (p,6),b6,bisect1),
     PDR_BIT_V (Pin (p,5),b5,bisect1),
     PDR_BIT_V (Pin (p,4),b4,bisect1),
     PDR_BIT_V (Pin (p,3),b3,bisect1),
     PDR_BIT_V (Pin (p,2),b2,bisect1),
     PDR_BIT_V (Pin (p,1),b1,bisect1),
     PDR_BIT_V (Pin (p,0),b0,bisect1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),0,b0),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),1,b1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),2,b2),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),3,b3),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),4,b4),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),5,b5),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),6,b6),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),7,b7)
    | @(bool (b7 == I),bool (b6 == I),bool (b5 == I),bool (b4 == I),
        bool (b3 == I),bool (b2 == I),bool (b1 == I),bool (b0 == I)))

// PODR

stadef PODR_BIT_HIGH   = I
stadef PODR_BIT_GROUND = O

absview PODR_BIT_V (Pin,bit,bisectional)

praxi {p:Pin}{b:bit} divide_podr_bit_v {r,s:bisectional | bisectional_half_b (r,s)}
      (PODR_BIT_V (p,b,r)) : (PODR_BIT_V (p,b,s),PODR_BIT_V (p,b,s))

praxi {p:Pin}{b:bit} merge_podr_bit_v {r,s,t:bisectional | bisectional_add_b (r,s,t)}
    (PODR_BIT_V (p,b,r), PODR_BIT_V (p,b,s)):(PODR_BIT_V (p,b,t))

dataview PODR_V (p:IOPort,bits) =
    {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}
    PODR_V (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) of
      (PODR_BIT_V (Pin (p,7),b7,bisect1), PODR_BIT_V (Pin (p,6),b6,bisect1),
       PODR_BIT_V (Pin (p,5),b5,bisect1), PODR_BIT_V (Pin (p,4),b4,bisect1),
       PODR_BIT_V (Pin (p,3),b3,bisect1), PODR_BIT_V (Pin (p,2),b2,bisect1),
       PODR_BIT_V (Pin (p,1),b1,bisect1), PODR_BIT_V (Pin (p,0),b0,bisect1))

absview PODR_PERMISSION (IOPort,bit_permissions)

viewdef PODR_PERMIT (p:IOPort,bs:bits) =
    [perms:bit_permissions]
    (PODR_PERMISSION (p,perms),BITS_PERMIT_CERTIFICATE (8,perms,bs))

fn {p:IOPort}{bs,cs:bits}
  writePODR (!PODR_V (p,bs) >> PODR_V (p,cs),
             !PODR_PERMIT (p,cs)
            | ioport_t p, bits_uint_t (8,cs)):<!wrt> void = "ext#"

fn {p:IOPort}{bs,cs:bits}{b:bit}
  changePODRBit {bn:int | bn < 8}
    (CHANGE_BIT_BITS (8,bs,bn,b,cs),
     !PODR_V (p,bs) >> PODR_V (p,cs),
     !PODR_PERMIT (p,cs)
    | pin_t (p,bn), bit_uint_t b):<!refwrt> void

fn {p:IOPort}{bs:bits}
  readPODR (!PODR_V (p,bs) | ioport_t p)
   :<!ref> bits_uint_t (8,bs)

fn podr2podr_bits {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}{v:int}
   (PODR_V (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) | bits_uint_t (8,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)))
   :(PODR_BIT_V (Pin (p,7),b7,bisect1),
     PODR_BIT_V (Pin (p,6),b6,bisect1),
     PODR_BIT_V (Pin (p,5),b5,bisect1),
     PODR_BIT_V (Pin (p,4),b4,bisect1),
     PODR_BIT_V (Pin (p,3),b3,bisect1),
     PODR_BIT_V (Pin (p,2),b2,bisect1),
     PODR_BIT_V (Pin (p,1),b1,bisect1),
     PODR_BIT_V (Pin (p,0),b0,bisect1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),0,b0),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),1,b1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),2,b2),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),3,b3),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),4,b4),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),5,b5),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),6,b6),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),7,b7)
    | @(bool (b7 == I),bool (b6 == I),bool (b5 == I),bool (b4 == I),
        bool (b3 == I),bool (b2 == I),bool (b1 == I),bool (b0 == I)))

// PIDR

absprop PIDR_BIT_P (Pin,bit)
dataprop PIDR_P (p:IOPort,bits) =
    {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}
    PIDR_P (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) of
      (PIDR_BIT_P (Pin (p,7),b7), PIDR_BIT_P (Pin (p,6),b6),
       PIDR_BIT_P (Pin (p,5),b5), PIDR_BIT_P (Pin (p,4),b4),
       PIDR_BIT_P (Pin (p,3),b3), PIDR_BIT_P (Pin (p,2),b2),
       PIDR_BIT_P (Pin (p,1),b1), PIDR_BIT_P (Pin (p,0),b0))

fn {p:IOPort} readPIDR (ioport_t p)
   :<!ref> [bs:bits] (PIDR_P (p,bs) | bits_uint_t (8,bs))

fn pidr2pidr_bits {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}{v:int}
   (PIDR_P (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) | bits_uint_t (8,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)))
   :(PIDR_BIT_P (Pin (p,7),b7),
     PIDR_BIT_P (Pin (p,6),b6),
     PIDR_BIT_P (Pin (p,5),b5),
     PIDR_BIT_P (Pin (p,4),b4),
     PIDR_BIT_P (Pin (p,3),b3),
     PIDR_BIT_P (Pin (p,2),b2),
     PIDR_BIT_P (Pin (p,1),b1),
     PIDR_BIT_P (Pin (p,0),b0),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),0,b0),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),1,b1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),2,b2),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),3,b3),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),4,b4),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),5,b5),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),6,b6),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),7,b7)
    | @(bool (b7 == I),bool (b6 == I),bool (b5 == I),bool (b4 == I),
        bool (b3 == I),bool (b2 == I),bool (b1 == I),bool (b0 == I)))

// GPIO

viewdef PinOutputView (p:Pin,output:bit) =
  [isel:bit][s,r,t:bisectional]
  (PFS_V (p,Bits8 (O,isel,O,O,O,O,O,O)),
   PMR_BIT_V (p,O,s),PDR_BIT_V (p,I,r),PODR_BIT_V (p,output,t))

absprop PinInput (Pin,bit)

fn readPinInput {p:IOPort}{n:int}{isel:bit}{s,r:bisectional}
   (!PFS_V (Pin (p,n),Bits8 (O,isel,O,O,O,O,O,O)),
   !PMR_BIT_V (Pin (p,n),O,s),!PDR_BIT_V (Pin (p,n),O,r) | pin_t (p,n))
   :<!ref> [input:bit] (PinInput (Pin (p,n),input) | bool (input==I))

stadef Bits8_all0 () : bits = Bits8 (O,O,O,O,O,O,O,O)
stadef Bits8_all1 () : bits = Bits8 (I,I,I,I,I,I,I,I)
stadef BitPerms8_all () : bit_permissions =
    BitPermissions8 (Permit,Permit,Permit,Permit,Permit,Permit,Permit,Permit,
                     Permit,Permit,Permit,Permit,Permit,Permit,Permit,Permit)

#if defined(MCU_RX110)
#if (MCU_Package_Pins == 64)
praxi rx110_initial_pmr_views () :
   (PMR_V (Port0,Bits8_all0),PMR_V (Port1,Bits8_all0),
    PMR_V (Port2,Bits8_all0),PMR_V (Port3,Bits8_all0),
    PMR_V (Port4,Bits8_all0),PMR_V (Port5,Bits8_all0),
    PMR_V (PortA,Bits8_all0),PMR_V (PortB,Bits8_all0),
    PMR_V (PortC,Bits8_all0),PMR_V (PortE,Bits8_all0),
    PMR_V (PortH,Bits8_all0),PMR_V (PortJ,Bits8_all0),
    PMR_PERMISSION (Port0,BitPerms8_all), PMR_PERMISSION (Port1,BitPerms8_all),
    PMR_PERMISSION (Port2,BitPerms8_all), PMR_PERMISSION (Port3,BitPerms8_all),
    PMR_PERMISSION (Port4,BitPerms8_all), PMR_PERMISSION (Port5,BitPerms8_all),
    PMR_PERMISSION (PortA,BitPerms8_all), PMR_PERMISSION (PortB,BitPerms8_all),
    PMR_PERMISSION (PortC,BitPerms8_all), PMR_PERMISSION (PortE,BitPerms8_all),
    PMR_PERMISSION (PortH,BitPerms8_all), PMR_PERMISSION (PortJ,BitPerms8_all)
   )

praxi rx110_consume_pmr_views (
    PMR_V (Port0,Bits8_all0),PMR_V (Port1,Bits8_all0),
    PMR_V (Port2,Bits8_all0),PMR_V (Port3,Bits8_all0),
    PMR_V (Port4,Bits8_all0),PMR_V (Port5,Bits8_all0),
    PMR_V (PortA,Bits8_all0),PMR_V (PortB,Bits8_all0),
    PMR_V (PortC,Bits8_all0),PMR_V (PortE,Bits8_all0),
    PMR_V (PortH,Bits8_all0),PMR_V (PortJ,Bits8_all0),
    PMR_PERMISSION (Port0,BitPerms8_all), PMR_PERMISSION (Port1,BitPerms8_all),
    PMR_PERMISSION (Port2,BitPerms8_all), PMR_PERMISSION (Port3,BitPerms8_all),
    PMR_PERMISSION (Port4,BitPerms8_all), PMR_PERMISSION (Port5,BitPerms8_all),
    PMR_PERMISSION (PortA,BitPerms8_all), PMR_PERMISSION (PortB,BitPerms8_all),
    PMR_PERMISSION (PortC,BitPerms8_all), PMR_PERMISSION (PortE,BitPerms8_all),
    PMR_PERMISSION (PortH,BitPerms8_all), PMR_PERMISSION (PortJ,BitPerms8_all)
   ): void

praxi rx110_initial_pfs_views ():
   (PFS_V (Pin (Port0,0),Bits8_all0),PFS_V (Pin (Port0,1),Bits8_all0),
    PFS_V (Pin (Port0,2),Bits8_all0),PFS_V (Pin (Port0,3),Bits8_all0),
    PFS_V (Pin (Port0,4),Bits8_all0),PFS_V (Pin (Port0,5),Bits8_all0),
    PFS_V (Pin (Port0,6),Bits8_all0),PFS_V (Pin (Port0,7),Bits8_all0),
    PFS_V (Pin (Port1,0),Bits8_all0),PFS_V (Pin (Port1,1),Bits8_all0),
    PFS_V (Pin (Port1,2),Bits8_all0),PFS_V (Pin (Port1,3),Bits8_all0),
    PFS_V (Pin (Port1,4),Bits8_all0),PFS_V (Pin (Port1,5),Bits8_all0),
    PFS_V (Pin (Port1,6),Bits8_all0),PFS_V (Pin (Port1,7),Bits8_all0),
    PFS_V (Pin (Port2,0),Bits8_all0),PFS_V (Pin (Port2,1),Bits8_all0),
    PFS_V (Pin (Port2,2),Bits8_all0),PFS_V (Pin (Port2,3),Bits8_all0),
    PFS_V (Pin (Port2,4),Bits8_all0),PFS_V (Pin (Port2,5),Bits8_all0),
    PFS_V (Pin (Port2,6),Bits8_all0),PFS_V (Pin (Port2,7),Bits8_all0),
    PFS_V (Pin (Port3,0),Bits8_all0),PFS_V (Pin (Port3,1),Bits8_all0),
    PFS_V (Pin (Port3,2),Bits8_all0),PFS_V (Pin (Port3,3),Bits8_all0),
    PFS_V (Pin (Port3,4),Bits8_all0),PFS_V (Pin (Port3,5),Bits8_all0),
    PFS_V (Pin (Port3,6),Bits8_all0),PFS_V (Pin (Port3,7),Bits8_all0),
    PFS_V (Pin (Port4,0),Bits8_all0),PFS_V (Pin (Port4,1),Bits8_all0),
    PFS_V (Pin (Port4,2),Bits8_all0),PFS_V (Pin (Port4,3),Bits8_all0),
    PFS_V (Pin (Port4,4),Bits8_all0),PFS_V (Pin (Port4,5),Bits8_all0),
    PFS_V (Pin (Port4,6),Bits8_all0),PFS_V (Pin (Port4,7),Bits8_all0),
    PFS_V (Pin (Port5,0),Bits8_all0),PFS_V (Pin (Port5,1),Bits8_all0),
    PFS_V (Pin (Port5,2),Bits8_all0),PFS_V (Pin (Port5,3),Bits8_all0),
    PFS_V (Pin (Port5,4),Bits8_all0),PFS_V (Pin (Port5,5),Bits8_all0),
    PFS_V (Pin (Port5,6),Bits8_all0),PFS_V (Pin (Port5,7),Bits8_all0),
    PFS_V (Pin (PortA,0),Bits8_all0),PFS_V (Pin (PortA,1),Bits8_all0),
    PFS_V (Pin (PortA,2),Bits8_all0),PFS_V (Pin (PortA,3),Bits8_all0),
    PFS_V (Pin (PortA,4),Bits8_all0),PFS_V (Pin (PortA,5),Bits8_all0),
    PFS_V (Pin (PortA,6),Bits8_all0),PFS_V (Pin (PortA,7),Bits8_all0),
    PFS_V (Pin (PortB,0),Bits8_all0),PFS_V (Pin (PortB,1),Bits8_all0),
    PFS_V (Pin (PortB,2),Bits8_all0),PFS_V (Pin (PortB,3),Bits8_all0),
    PFS_V (Pin (PortB,4),Bits8_all0),PFS_V (Pin (PortB,5),Bits8_all0),
    PFS_V (Pin (PortB,6),Bits8_all0),PFS_V (Pin (PortB,7),Bits8_all0),
    PFS_V (Pin (PortC,0),Bits8_all0),PFS_V (Pin (PortC,1),Bits8_all0),
    PFS_V (Pin (PortC,2),Bits8_all0),PFS_V (Pin (PortC,3),Bits8_all0),
    PFS_V (Pin (PortC,4),Bits8_all0),PFS_V (Pin (PortC,5),Bits8_all0),
    PFS_V (Pin (PortC,6),Bits8_all0),PFS_V (Pin (PortC,7),Bits8_all0),
    PFS_V (Pin (PortE,0),Bits8_all0),PFS_V (Pin (PortE,1),Bits8_all0),
    PFS_V (Pin (PortE,2),Bits8_all0),PFS_V (Pin (PortE,3),Bits8_all0),
    PFS_V (Pin (PortE,4),Bits8_all0),PFS_V (Pin (PortE,5),Bits8_all0),
    PFS_V (Pin (PortE,6),Bits8_all0),PFS_V (Pin (PortE,7),Bits8_all0),
    PFS_V (Pin (PortH,0),Bits8_all0),PFS_V (Pin (PortH,1),Bits8_all0),
    PFS_V (Pin (PortH,2),Bits8_all0),PFS_V (Pin (PortH,3),Bits8_all0),
    PFS_V (Pin (PortH,4),Bits8_all0),PFS_V (Pin (PortH,5),Bits8_all0),
    PFS_V (Pin (PortH,6),Bits8_all0),PFS_V (Pin (PortH,7),Bits8_all0),
    PFS_V (Pin (PortJ,0),Bits8_all0),PFS_V (Pin (PortJ,1),Bits8_all0),
    PFS_V (Pin (PortJ,2),Bits8_all0),PFS_V (Pin (PortJ,3),Bits8_all0),
    PFS_V (Pin (PortJ,4),Bits8_all0),PFS_V (Pin (PortJ,5),Bits8_all0),
    PFS_V (Pin (PortJ,6),Bits8_all0),PFS_V (Pin (PortJ,7),Bits8_all0)
   )

praxi rx110_consume_pfs_views (
    PFS_V (Pin (Port0,0),Bits8_all0),PFS_V (Pin (Port0,1),Bits8_all0),
    PFS_V (Pin (Port0,2),Bits8_all0),PFS_V (Pin (Port0,3),Bits8_all0),
    PFS_V (Pin (Port0,4),Bits8_all0),PFS_V (Pin (Port0,5),Bits8_all0),
    PFS_V (Pin (Port0,6),Bits8_all0),PFS_V (Pin (Port0,7),Bits8_all0),
    PFS_V (Pin (Port1,0),Bits8_all0),PFS_V (Pin (Port1,1),Bits8_all0),
    PFS_V (Pin (Port1,2),Bits8_all0),PFS_V (Pin (Port1,3),Bits8_all0),
    PFS_V (Pin (Port1,4),Bits8_all0),PFS_V (Pin (Port1,5),Bits8_all0),
    PFS_V (Pin (Port1,6),Bits8_all0),PFS_V (Pin (Port1,7),Bits8_all0),
    PFS_V (Pin (Port2,0),Bits8_all0),PFS_V (Pin (Port2,1),Bits8_all0),
    PFS_V (Pin (Port2,2),Bits8_all0),PFS_V (Pin (Port2,3),Bits8_all0),
    PFS_V (Pin (Port2,4),Bits8_all0),PFS_V (Pin (Port2,5),Bits8_all0),
    PFS_V (Pin (Port2,6),Bits8_all0),PFS_V (Pin (Port2,7),Bits8_all0),
    PFS_V (Pin (Port3,0),Bits8_all0),PFS_V (Pin (Port3,1),Bits8_all0),
    PFS_V (Pin (Port3,2),Bits8_all0),PFS_V (Pin (Port3,3),Bits8_all0),
    PFS_V (Pin (Port3,4),Bits8_all0),PFS_V (Pin (Port3,5),Bits8_all0),
    PFS_V (Pin (Port3,6),Bits8_all0),PFS_V (Pin (Port3,7),Bits8_all0),
    PFS_V (Pin (Port4,0),Bits8_all0),PFS_V (Pin (Port4,1),Bits8_all0),
    PFS_V (Pin (Port4,2),Bits8_all0),PFS_V (Pin (Port4,3),Bits8_all0),
    PFS_V (Pin (Port4,4),Bits8_all0),PFS_V (Pin (Port4,5),Bits8_all0),
    PFS_V (Pin (Port4,6),Bits8_all0),PFS_V (Pin (Port4,7),Bits8_all0),
    PFS_V (Pin (Port5,0),Bits8_all0),PFS_V (Pin (Port5,1),Bits8_all0),
    PFS_V (Pin (Port5,2),Bits8_all0),PFS_V (Pin (Port5,3),Bits8_all0),
    PFS_V (Pin (Port5,4),Bits8_all0),PFS_V (Pin (Port5,5),Bits8_all0),
    PFS_V (Pin (Port5,6),Bits8_all0),PFS_V (Pin (Port5,7),Bits8_all0),
    PFS_V (Pin (PortA,0),Bits8_all0),PFS_V (Pin (PortA,1),Bits8_all0),
    PFS_V (Pin (PortA,2),Bits8_all0),PFS_V (Pin (PortA,3),Bits8_all0),
    PFS_V (Pin (PortA,4),Bits8_all0),PFS_V (Pin (PortA,5),Bits8_all0),
    PFS_V (Pin (PortA,6),Bits8_all0),PFS_V (Pin (PortA,7),Bits8_all0),
    PFS_V (Pin (PortB,0),Bits8_all0),PFS_V (Pin (PortB,1),Bits8_all0),
    PFS_V (Pin (PortB,2),Bits8_all0),PFS_V (Pin (PortB,3),Bits8_all0),
    PFS_V (Pin (PortB,4),Bits8_all0),PFS_V (Pin (PortB,5),Bits8_all0),
    PFS_V (Pin (PortB,6),Bits8_all0),PFS_V (Pin (PortB,7),Bits8_all0),
    PFS_V (Pin (PortC,0),Bits8_all0),PFS_V (Pin (PortC,1),Bits8_all0),
    PFS_V (Pin (PortC,2),Bits8_all0),PFS_V (Pin (PortC,3),Bits8_all0),
    PFS_V (Pin (PortC,4),Bits8_all0),PFS_V (Pin (PortC,5),Bits8_all0),
    PFS_V (Pin (PortC,6),Bits8_all0),PFS_V (Pin (PortC,7),Bits8_all0),
    PFS_V (Pin (PortE,0),Bits8_all0),PFS_V (Pin (PortE,1),Bits8_all0),
    PFS_V (Pin (PortE,2),Bits8_all0),PFS_V (Pin (PortE,3),Bits8_all0),
    PFS_V (Pin (PortE,4),Bits8_all0),PFS_V (Pin (PortE,5),Bits8_all0),
    PFS_V (Pin (PortE,6),Bits8_all0),PFS_V (Pin (PortE,7),Bits8_all0),
    PFS_V (Pin (PortH,0),Bits8_all0),PFS_V (Pin (PortH,1),Bits8_all0),
    PFS_V (Pin (PortH,2),Bits8_all0),PFS_V (Pin (PortH,3),Bits8_all0),
    PFS_V (Pin (PortH,4),Bits8_all0),PFS_V (Pin (PortH,5),Bits8_all0),
    PFS_V (Pin (PortH,6),Bits8_all0),PFS_V (Pin (PortH,7),Bits8_all0),
    PFS_V (Pin (PortJ,0),Bits8_all0),PFS_V (Pin (PortJ,1),Bits8_all0),
    PFS_V (Pin (PortJ,2),Bits8_all0),PFS_V (Pin (PortJ,3),Bits8_all0),
    PFS_V (Pin (PortJ,4),Bits8_all0),PFS_V (Pin (PortJ,5),Bits8_all0),
    PFS_V (Pin (PortJ,6),Bits8_all0),PFS_V (Pin (PortJ,7),Bits8_all0)
   ): void

praxi rx110_initial_pdr_views ():
   (PDR_V (Port0,Bits8_all0),PDR_V (Port1,Bits8_all0),
    PDR_V (Port2,Bits8_all0),PDR_V (Port3,Bits8_all0),
    PDR_V (Port4,Bits8_all0),PDR_V (Port5,Bits8_all0),
    PDR_V (PortA,Bits8_all0),PDR_V (PortB,Bits8_all0),
    PDR_V (PortC,Bits8_all0),PDR_V (PortE,Bits8_all0),
    PDR_V (PortH,Bits8_all0),PDR_V (PortJ,Bits8_all0),
    PDR_PERMISSION (Port0,BitPerms8_all), PDR_PERMISSION (Port1,BitPerms8_all),
    PDR_PERMISSION (Port2,BitPerms8_all), PDR_PERMISSION (Port3,BitPerms8_all),
    PDR_PERMISSION (Port4,BitPerms8_all), PDR_PERMISSION (Port5,BitPerms8_all),
    PDR_PERMISSION (PortA,BitPerms8_all), PDR_PERMISSION (PortB,BitPerms8_all),
    PDR_PERMISSION (PortC,BitPerms8_all), PDR_PERMISSION (PortE,BitPerms8_all),
    PDR_PERMISSION (PortH,BitPerms8_all), PDR_PERMISSION (PortJ,BitPerms8_all)
   )

praxi rx110_consume_pdr_views (
    PDR_V (Port0,Bits8_all0),PDR_V (Port1,Bits8_all0),
    PDR_V (Port2,Bits8_all0),PDR_V (Port3,Bits8_all0),
    PDR_V (Port4,Bits8_all0),PDR_V (Port5,Bits8_all0),
    PDR_V (PortA,Bits8_all0),PDR_V (PortB,Bits8_all0),
    PDR_V (PortC,Bits8_all0),PDR_V (PortE,Bits8_all0),
    PDR_V (PortH,Bits8_all0),PDR_V (PortJ,Bits8_all0),
    PDR_PERMISSION (Port0,BitPerms8_all), PDR_PERMISSION (Port1,BitPerms8_all),
    PDR_PERMISSION (Port2,BitPerms8_all), PDR_PERMISSION (Port3,BitPerms8_all),
    PDR_PERMISSION (Port4,BitPerms8_all), PDR_PERMISSION (Port5,BitPerms8_all),
    PDR_PERMISSION (PortA,BitPerms8_all), PDR_PERMISSION (PortB,BitPerms8_all),
    PDR_PERMISSION (PortC,BitPerms8_all), PDR_PERMISSION (PortE,BitPerms8_all),
    PDR_PERMISSION (PortH,BitPerms8_all), PDR_PERMISSION (PortJ,BitPerms8_all)
   ): void

praxi rx110_initial_podr_views ():
   (PODR_V (Port0,Bits8_all0),PODR_V (Port1,Bits8_all0),
    PODR_V (Port2,Bits8_all0),PODR_V (Port3,Bits8_all0),
    PODR_V (Port4,Bits8_all0),PODR_V (Port5,Bits8_all0),
    PODR_V (PortA,Bits8_all0),PODR_V (PortB,Bits8_all0),
    PODR_V (PortC,Bits8_all0),PODR_V (PortE,Bits8_all0),
    PODR_V (PortH,Bits8_all0),PODR_V (PortJ,Bits8_all0),
    PODR_PERMISSION (Port0,BitPerms8_all), PODR_PERMISSION (Port1,BitPerms8_all),
    PODR_PERMISSION (Port2,BitPerms8_all), PODR_PERMISSION (Port3,BitPerms8_all),
    PODR_PERMISSION (Port4,BitPerms8_all), PODR_PERMISSION (Port5,BitPerms8_all),
    PODR_PERMISSION (PortA,BitPerms8_all), PODR_PERMISSION (PortB,BitPerms8_all),
    PODR_PERMISSION (PortC,BitPerms8_all), PODR_PERMISSION (PortE,BitPerms8_all),
    PODR_PERMISSION (PortH,BitPerms8_all), PODR_PERMISSION (PortJ,BitPerms8_all)
   )

praxi rx110_consume_podr_views (
    PODR_V (Port0,Bits8_all0),PODR_V (Port1,Bits8_all0),
    PODR_V (Port2,Bits8_all0),PODR_V (Port3,Bits8_all0),
    PODR_V (Port4,Bits8_all0),PODR_V (Port5,Bits8_all0),
    PODR_V (PortA,Bits8_all0),PODR_V (PortB,Bits8_all0),
    PODR_V (PortC,Bits8_all0),PODR_V (PortE,Bits8_all0),
    PODR_V (PortH,Bits8_all0),PODR_V (PortJ,Bits8_all0),
    PODR_PERMISSION (Port0,BitPerms8_all), PODR_PERMISSION (Port1,BitPerms8_all),
    PODR_PERMISSION (Port2,BitPerms8_all), PODR_PERMISSION (Port3,BitPerms8_all),
    PODR_PERMISSION (Port4,BitPerms8_all), PODR_PERMISSION (Port5,BitPerms8_all),
    PODR_PERMISSION (PortA,BitPerms8_all), PODR_PERMISSION (PortB,BitPerms8_all),
    PODR_PERMISSION (PortC,BitPerms8_all), PODR_PERMISSION (PortE,BitPerms8_all),
    PODR_PERMISSION (PortH,BitPerms8_all), PODR_PERMISSION (PortJ,BitPerms8_all)
   ): void

#endif
#endif

// Pin control drivers

staload "Bit.sats"

// Target MCU
#define MCU_RX110
#define MCU_Package_Pins 64

datasort IOPort =
 | Port0 of () | Port1 of () | Port2 of () | Port3 of ()
 | Port4 of () | Port5 of () | Port6 of () | Port7 of ()
 | Port8 of () | Port9 of () | PortA of () | PortB of ()
 | PortC of () | PortD of () | PortE of () | PortF of ()
 | PortG of () | PortH of () | PortI of () | PortJ of ()

stacst IOPort2int (p:IOPort):int
praxi ioport_eq_int ():[
    IOPort2int Port0 == 0 && IOPort2int Port1 == 1 &&
    IOPort2int Port2 == 2 && IOPort2int Port3 == 3 &&
    IOPort2int Port4 == 4 && IOPort2int Port5 == 5 &&
    IOPort2int Port6 == 6 && IOPort2int Port7 == 7 &&
    IOPort2int Port8 == 8 && IOPort2int Port9 == 9 &&
    IOPort2int PortA == 10 && IOPort2int PortB == 11 &&
    IOPort2int PortC == 12 && IOPort2int PortD == 13 &&
    IOPort2int PortE == 14 && IOPort2int PortF == 15 &&
    IOPort2int PortG == 16 && IOPort2int PortH == 17 &&
    IOPort2int PortI == 18 && IOPort2int PortJ == 19
  ] void

typedef ioport_t (p:IOPort) = int (IOPort2int p)

datasort Pin = Pin of (IOPort,int)

typedef pin_t (p:IOPort,n:int) = @(ioport_t p, uint n)

stadef P0 (n:int):Pin = Pin (Port0,n)
stadef P1 (n:int):Pin = Pin (Port1,n)
stadef P2 (n:int):Pin = Pin (Port2,n)
stadef P3 (n:int):Pin = Pin (Port3,n)
stadef P4 (n:int):Pin = Pin (Port4,n)
stadef P5 (n:int):Pin = Pin (Port5,n)
stadef P6 (n:int):Pin = Pin (Port6,n)
stadef P7 (n:int):Pin = Pin (Port7,n)
stadef P8 (n:int):Pin = Pin (Port8,n)
stadef P9 (n:int):Pin = Pin (Port9,n)
stadef PA (n:int):Pin = Pin (PortA,n)
stadef PB (n:int):Pin = Pin (PortB,n)
stadef PC (n:int):Pin = Pin (PortC,n)
stadef PD (n:int):Pin = Pin (PortD,n)
stadef PE (n:int):Pin = Pin (PortE,n)
stadef PF (n:int):Pin = Pin (PortF,n)
stadef PG (n:int):Pin = Pin (PortG,n)
stadef PH (n:int):Pin = Pin (PortH,n)
stadef PI (n:int):Pin = Pin (PortI,n)
stadef PJ (n:int):Pin = Pin (PortJ,n)

// Special form of fractional (n/2^m only)
datasort bisectional = bisectional of (int,int)

stadef bisect1 = bisectional (1,0)

dataprop EQBISECTIONAL (bisectional, bisectional) = {r:bisectional} EQBISECTIONAL (r, r)
praxi eqbisectional_make {r,s:bisectional | r == s} () : EQBISECTIONAL (r,s)
praxi bisectional_eq_refl {r:bisectional} () : [r == r] void
praxi bisectional_eq_reduce {n,m:int | n <= INTMAX_HALF} ():
      [bisectional (n,m) == bisectional (n+n,m+1)] void

stacst bisectional_add_b : (bisectional,bisectional,bisectional) -> bool

praxi {n1,n2,m:int} bisectional_add_axi ():
      [bisectional_add_b (bisectional (n1,m),bisectional (n2,m),bisectional (n1+n2,m))] unit_p

praxi bisectional_add_axi_rev
 {n1,n2,n3,m:int | bisectional_add_b (bisectional (n1,m),bisectional (n2,m),bisectional (n3,m))}(): [n1+n2==n3] unit_p

stacst bisectional_half_b : (bisectional,bisectional) -> bool

praxi {n,m:int} bisectional_half_axi ():
      [bisectional_half_b (bisectional (n,m),bisectional (n,m+1))] unit_p

praxi {n,m,l:int} bisectional_half_axi_rev {bisectional_half_b (bisectional (n,m),bisectional (n,l))}(): [m+1==l] unit_p

// PMR

absview PMR_BIT_V (Pin,bit,bisectional)

praxi {p:Pin}{b:bit} divide_pmr_bit_v {r,s:bisectional | bisectional_half_b (r,s)}
      (PMR_BIT_V (p,b,r)) : (PMR_BIT_V (p,b,s),PMR_BIT_V (p,b,s))

praxi {p:Pin}{b:bit} merge_pmr_bit_v {r,s,t:bisectional | bisectional_add_b (r,s,t)}
      (PMR_BIT_V (p,b,r), PMR_BIT_V (p,b,s)):(PMR_BIT_V (p,b,t))

stadef PMR_BIT_IOPORT     = O
stadef PMR_BIT_PERIPHERAL = I

dataview PMR_V (IOPort,bits) =
    {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}
    PMR_V (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) of
      (PMR_BIT_V (Pin (p,7),b7,bisect1), PMR_BIT_V (Pin (p,6),b6,bisect1),
       PMR_BIT_V (Pin (p,5),b5,bisect1), PMR_BIT_V (Pin (p,4),b4,bisect1),
       PMR_BIT_V (Pin (p,3),b3,bisect1), PMR_BIT_V (Pin (p,2),b2,bisect1),
       PMR_BIT_V (Pin (p,1),b1,bisect1), PMR_BIT_V (Pin (p,0),b0,bisect1))

absview PMR_PERMISSION (IOPort,bit_permissions)

viewdef PMR_PERMIT (p:IOPort,bs:bits) =
    [perms:bit_permissions]
    (PMR_PERMISSION (p,perms),BITS_PERMIT_CERTIFICATE (8,perms,bs))

fn {p:IOPort}{bs,cs:bits}
  writePMR (!PMR_V (p,bs) >> PMR_V (p,cs),
            !PMR_PERMIT (p,cs)
           | ioport_t p,bits_uint_t (8,cs))
   :<!wrt> void

fn {p:IOPort}{bs,cs:bits}{b:bit}
  changePMRBit {bn:int | bn < 8}
    (CHANGE_BIT_BITS (8,bs,bn,b,cs),
     !PMR_V (p,bs) >> PMR_V (p,cs),
     !PMR_PERMIT (p,cs)
    | pin_t (p,bn), bit_uint_t b):<!refwrt> void

fn {p:IOPort}{bs:bits}
  readPMR (!PMR_V (p,bs) | p:ioport_t p)
   :<!ref> bits_uint_t (8,bs)

fn pmr2pmr_bits {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}{v:int}
   (PMR_V (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) | bits_uint_t (8,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)))
   :(PMR_BIT_V (Pin (p,7),b7,bisect1),
     PMR_BIT_V (Pin (p,6),b6,bisect1),
     PMR_BIT_V (Pin (p,5),b5,bisect1),
     PMR_BIT_V (Pin (p,4),b4,bisect1),
     PMR_BIT_V (Pin (p,3),b3,bisect1),
     PMR_BIT_V (Pin (p,2),b2,bisect1),
     PMR_BIT_V (Pin (p,1),b1,bisect1),
     PMR_BIT_V (Pin (p,0),b0,bisect1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),0,b0),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),1,b1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),2,b2),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),3,b3),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),4,b4),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),5,b5),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),6,b6),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),7,b7)
    | @(bool (b7 == I),bool (b6 == I),bool (b5 == I),bool (b4 == I),
        bool (b3 == I),bool (b2 == I),bool (b1 == I),bool (b0 == I)))

// The PFS function is postponed.
// Define only PFS_V so that other functions can acquire the state of the initial value
(*
// PWPR

absview PWPR_V (bits)

fn writePWPR {
    pfswe,b0wi,PFSWE,B0WI:bit
    | (pfswe==PFSWE) || (b0wi==O && B0WI==O)
  }(
    !PWPR_V (Bits8 (b0wi,pfswe,O,O,O,O,O,O)) >>
     PWPR_V (Bits8 (B0WI,PFSWE,O,O,O,O,O,O)) |
     bits8int_t (B0WI,PFSWE,O,O,O,O,O,O)
  ):void

fn {pfswe,b0wi:bit}
  readPWPR (!PWPR_V (Bits8 (b0wi,pfswe,O,O,O,O,O,O)))
   :bits8int_t (b0wi,pfswe,O,O,O,O,O,O)
*)

// PFS

absview PFS_V (Pin,bits)
(*
fn {p:IOPort}{pnum:int}
   {b0wi,asel,isel,psel4,psel3,psel2,psel1,psel0:bit}
  writePFS (
    !PWPR_V (Bits8 (O,O,O,O,O,O,I,b0wi)),
    !PFS_V (Pin (p,pnum),Bits8 (asel,isel,O,psel4,psel3,psel2,psel1,psel0)) >>
     PFS_V (Pin (p,pnum),Bits8 (O,O,O,O,O,O,O,O)),
    !PMR_BIT_V (Pin (p,pnum), O)
  | pin_t (p,pnum),
    bits8int_t (O,O,O,O,O,O,O,O)
  )
  :void

fn {p:IOPort}{pnum:int}{asel,isel,psel4,psel3,psel2,psel1,psel0:bit}
  readPFS (
    !PFS_V (Pin (p,pnum),Bits8 (asel,isel,O,psel4,psel3,psel2,psel1,psel0))
  | pin_t (p, pnum)
  )
  :bits8int_t (asel,isel,O,psel4,psel3,psel2,psel1,psel0)
*)

// TODO Create duplicate prohibited view of peripheral devices.
//      Issues view with no device assignment pins first.
//       Propriety of writing / disabling per pins is required.

// PDR

stadef PDR_BIT_WRITABLE  = I
stadef PDR_BIT_READ_ONLY = O

absview PDR_BIT_V (Pin,bit,bisectional)

praxi {p:Pin}{b:bit} divide_pdr_bit_v {r,s:bisectional | bisectional_half_b (r,s)}
      (PDR_BIT_V (p,b,r)) : (PDR_BIT_V (p,b,s),PDR_BIT_V (p,b,s))

praxi {p:Pin}{b:bit} merge_pdr_bit_v {r,s,t:bisectional | bisectional_add_b (r,s,t)}
      (PDR_BIT_V (p,b,r), PDR_BIT_V (p,b,s)):(PDR_BIT_V (p,b,t))

dataview PDR_V (p:IOPort,bits) =
    {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}
    PDR_V (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) of
      (PDR_BIT_V (Pin (p,7),b7,bisect1), PDR_BIT_V (Pin (p,6),b6,bisect1),
       PDR_BIT_V (Pin (p,5),b5,bisect1), PDR_BIT_V (Pin (p,4),b4,bisect1),
       PDR_BIT_V (Pin (p,3),b3,bisect1), PDR_BIT_V (Pin (p,2),b2,bisect1),
       PDR_BIT_V (Pin (p,1),b1,bisect1), PDR_BIT_V (Pin (p,0),b0,bisect1))

absview PDR_PERMISSION (IOPort,bit_permissions)

viewdef PDR_PERMIT (p:IOPort,bs:bits) =
    [perms:bit_permissions]
    (PDR_PERMISSION (p,perms),BITS_PERMIT_CERTIFICATE (8,perms,bs))

fn {p:IOPort}{bs,cs:bits}
  writePDR (!PDR_V (p,bs) >> PDR_V (p,cs),
            !PDR_PERMIT (p,cs)
           | ioport_t p,bits_uint_t (8,cs)):<!wrt> void

fn {p:IOPort}{bs,cs:bits}{b:bit}
  changePDRBit {bn:int | bn < 8}
    (CHANGE_BIT_BITS (8,bs,bn,b,cs),
     !PDR_V (p,bs) >> PDR_V (p,cs),
     !PDR_PERMIT (p,cs)
    | pin_t (p,bn), bit_uint_t b):<!refwrt> void

fn {p:IOPort}{bs:bits}
  readPDR (!PDR_V (p,bs) | ioport_t p)
   :<!ref> bits_uint_t (8,bs)

fn pdr2pdr_bits {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}{v:int}
   (PDR_V (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) | bits_uint_t (8,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)))
   :(PDR_BIT_V (Pin (p,7),b7,bisect1),
     PDR_BIT_V (Pin (p,6),b6,bisect1),
     PDR_BIT_V (Pin (p,5),b5,bisect1),
     PDR_BIT_V (Pin (p,4),b4,bisect1),
     PDR_BIT_V (Pin (p,3),b3,bisect1),
     PDR_BIT_V (Pin (p,2),b2,bisect1),
     PDR_BIT_V (Pin (p,1),b1,bisect1),
     PDR_BIT_V (Pin (p,0),b0,bisect1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),0,b0),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),1,b1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),2,b2),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),3,b3),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),4,b4),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),5,b5),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),6,b6),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),7,b7)
    | @(bool (b7 == I),bool (b6 == I),bool (b5 == I),bool (b4 == I),
        bool (b3 == I),bool (b2 == I),bool (b1 == I),bool (b0 == I)))

// PODR

stadef PODR_BIT_HIGH   = I
stadef PODR_BIT_GROUND = O

absview PODR_BIT_V (Pin,bit,bisectional)

praxi {p:Pin}{b:bit} divide_podr_bit_v {r,s:bisectional | bisectional_half_b (r,s)}
      (PODR_BIT_V (p,b,r)) : (PODR_BIT_V (p,b,s),PODR_BIT_V (p,b,s))

praxi {p:Pin}{b:bit} merge_podr_bit_v {r,s,t:bisectional | bisectional_add_b (r,s,t)}
    (PODR_BIT_V (p,b,r), PODR_BIT_V (p,b,s)):(PODR_BIT_V (p,b,t))

dataview PODR_V (p:IOPort,bits) =
    {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}
    PODR_V (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) of
      (PODR_BIT_V (Pin (p,7),b7,bisect1), PODR_BIT_V (Pin (p,6),b6,bisect1),
       PODR_BIT_V (Pin (p,5),b5,bisect1), PODR_BIT_V (Pin (p,4),b4,bisect1),
       PODR_BIT_V (Pin (p,3),b3,bisect1), PODR_BIT_V (Pin (p,2),b2,bisect1),
       PODR_BIT_V (Pin (p,1),b1,bisect1), PODR_BIT_V (Pin (p,0),b0,bisect1))

absview PODR_PERMISSION (IOPort,bit_permissions)

viewdef PODR_PERMIT (p:IOPort,bs:bits) =
    [perms:bit_permissions]
    (PODR_PERMISSION (p,perms),BITS_PERMIT_CERTIFICATE (8,perms,bs))

fn {p:IOPort}{bs,cs:bits}
  writePODR (!PODR_V (p,bs) >> PODR_V (p,cs),
             !PODR_PERMIT (p,cs)
            | ioport_t p, bits_uint_t (8,cs)):<!wrt> void = "ext#"

fn {p:IOPort}{bs,cs:bits}{b:bit}
  changePODRBit {bn:int | bn < 8}
    (CHANGE_BIT_BITS (8,bs,bn,b,cs),
     !PODR_V (p,bs) >> PODR_V (p,cs),
     !PODR_PERMIT (p,cs)
    | pin_t (p,bn), bit_uint_t b):<!refwrt> void

fn {p:IOPort}{bs:bits}
  readPODR (!PODR_V (p,bs) | ioport_t p)
   :<!ref> bits_uint_t (8,bs)

fn podr2podr_bits {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}{v:int}
   (PODR_V (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) | bits_uint_t (8,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)))
   :(PODR_BIT_V (Pin (p,7),b7,bisect1),
     PODR_BIT_V (Pin (p,6),b6,bisect1),
     PODR_BIT_V (Pin (p,5),b5,bisect1),
     PODR_BIT_V (Pin (p,4),b4,bisect1),
     PODR_BIT_V (Pin (p,3),b3,bisect1),
     PODR_BIT_V (Pin (p,2),b2,bisect1),
     PODR_BIT_V (Pin (p,1),b1,bisect1),
     PODR_BIT_V (Pin (p,0),b0,bisect1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),0,b0),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),1,b1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),2,b2),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),3,b3),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),4,b4),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),5,b5),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),6,b6),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),7,b7)
    | @(bool (b7 == I),bool (b6 == I),bool (b5 == I),bool (b4 == I),
        bool (b3 == I),bool (b2 == I),bool (b1 == I),bool (b0 == I)))

// PIDR

absprop PIDR_BIT_P (Pin,bit)
dataprop PIDR_P (p:IOPort,bits) =
    {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}
    PIDR_P (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) of
      (PIDR_BIT_P (Pin (p,7),b7), PIDR_BIT_P (Pin (p,6),b6),
       PIDR_BIT_P (Pin (p,5),b5), PIDR_BIT_P (Pin (p,4),b4),
       PIDR_BIT_P (Pin (p,3),b3), PIDR_BIT_P (Pin (p,2),b2),
       PIDR_BIT_P (Pin (p,1),b1), PIDR_BIT_P (Pin (p,0),b0))

fn {p:IOPort} readPIDR (ioport_t p)
   :<!ref> [bs:bits] (PIDR_P (p,bs) | bits_uint_t (8,bs))

fn pidr2pidr_bits {p:IOPort}{b7,b6,b5,b4,b3,b2,b1,b0:bit}{v:int}
   (PIDR_P (p,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)) | bits_uint_t (8,Bits8 (b7,b6,b5,b4,b3,b2,b1,b0)))
   :(PIDR_BIT_P (Pin (p,7),b7),
     PIDR_BIT_P (Pin (p,6),b6),
     PIDR_BIT_P (Pin (p,5),b5),
     PIDR_BIT_P (Pin (p,4),b4),
     PIDR_BIT_P (Pin (p,3),b3),
     PIDR_BIT_P (Pin (p,2),b2),
     PIDR_BIT_P (Pin (p,1),b1),
     PIDR_BIT_P (Pin (p,0),b0),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),0,b0),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),1,b1),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),2,b2),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),3,b3),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),4,b4),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),5,b5),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),6,b6),
     TEST_BIT_BITS (Bits8 (b7,b6,b5,b4,b3,b2,b1,b0),7,b7)
    | @(bool (b7 == I),bool (b6 == I),bool (b5 == I),bool (b4 == I),
        bool (b3 == I),bool (b2 == I),bool (b1 == I),bool (b0 == I)))

// GPIO

viewdef PinOutputView (p:Pin,output:bit) =
  [isel:bit][s,r,t:bisectional]
  (PFS_V (p,Bits8 (O,isel,O,O,O,O,O,O)),
   PMR_BIT_V (p,O,s),PDR_BIT_V (p,I,r),PODR_BIT_V (p,output,t))

absprop PinInput (Pin,bit)

fn readPinInput {p:IOPort}{n:int}{isel:bit}{s,r:bisectional}
   (!PFS_V (Pin (p,n),Bits8 (O,isel,O,O,O,O,O,O)),
   !PMR_BIT_V (Pin (p,n),O,s),!PDR_BIT_V (Pin (p,n),O,r) | pin_t (p,n))
   :<!ref> [input:bit] (PinInput (Pin (p,n),input) | bool (input==I))

stadef Bits8_all0 () : bits = Bits8 (O,O,O,O,O,O,O,O)
stadef Bits8_all1 () : bits = Bits8 (I,I,I,I,I,I,I,I)
stadef BitPerms8_all () : bit_permissions =
    BitPermissions8 (Permit,Permit,Permit,Permit,Permit,Permit,Permit,Permit,
                     Permit,Permit,Permit,Permit,Permit,Permit,Permit,Permit)

#if defined(MCU_RX110)
#if (MCU_Package_Pins == 64)
praxi rx110_initial_pmr_views () :
   (PMR_V (Port0,Bits8_all0),PMR_V (Port1,Bits8_all0),
    PMR_V (Port2,Bits8_all0),PMR_V (Port3,Bits8_all0),
    PMR_V (Port4,Bits8_all0),PMR_V (Port5,Bits8_all0),
    PMR_V (PortA,Bits8_all0),PMR_V (PortB,Bits8_all0),
    PMR_V (PortC,Bits8_all0),PMR_V (PortE,Bits8_all0),
    PMR_V (PortH,Bits8_all0),PMR_V (PortJ,Bits8_all0),
    PMR_PERMISSION (Port0,BitPerms8_all), PMR_PERMISSION (Port1,BitPerms8_all),
    PMR_PERMISSION (Port2,BitPerms8_all), PMR_PERMISSION (Port3,BitPerms8_all),
    PMR_PERMISSION (Port4,BitPerms8_all), PMR_PERMISSION (Port5,BitPerms8_all),
    PMR_PERMISSION (PortA,BitPerms8_all), PMR_PERMISSION (PortB,BitPerms8_all),
    PMR_PERMISSION (PortC,BitPerms8_all), PMR_PERMISSION (PortE,BitPerms8_all),
    PMR_PERMISSION (PortH,BitPerms8_all), PMR_PERMISSION (PortJ,BitPerms8_all)
   )

praxi rx110_consume_pmr_views (
    PMR_V (Port0,Bits8_all0),PMR_V (Port1,Bits8_all0),
    PMR_V (Port2,Bits8_all0),PMR_V (Port3,Bits8_all0),
    PMR_V (Port4,Bits8_all0),PMR_V (Port5,Bits8_all0),
    PMR_V (PortA,Bits8_all0),PMR_V (PortB,Bits8_all0),
    PMR_V (PortC,Bits8_all0),PMR_V (PortE,Bits8_all0),
    PMR_V (PortH,Bits8_all0),PMR_V (PortJ,Bits8_all0),
    PMR_PERMISSION (Port0,BitPerms8_all), PMR_PERMISSION (Port1,BitPerms8_all),
    PMR_PERMISSION (Port2,BitPerms8_all), PMR_PERMISSION (Port3,BitPerms8_all),
    PMR_PERMISSION (Port4,BitPerms8_all), PMR_PERMISSION (Port5,BitPerms8_all),
    PMR_PERMISSION (PortA,BitPerms8_all), PMR_PERMISSION (PortB,BitPerms8_all),
    PMR_PERMISSION (PortC,BitPerms8_all), PMR_PERMISSION (PortE,BitPerms8_all),
    PMR_PERMISSION (PortH,BitPerms8_all), PMR_PERMISSION (PortJ,BitPerms8_all)
   ): void

praxi rx110_initial_pfs_views (): //<lin>
   (PFS_V (Pin (Port0,0),Bits8_all0),PFS_V (Pin (Port0,1),Bits8_all0),
    PFS_V (Pin (Port0,2),Bits8_all0),PFS_V (Pin (Port0,3),Bits8_all0),
    PFS_V (Pin (Port0,4),Bits8_all0),PFS_V (Pin (Port0,5),Bits8_all0),
    PFS_V (Pin (Port0,6),Bits8_all0),PFS_V (Pin (Port0,7),Bits8_all0),
    PFS_V (Pin (Port1,0),Bits8_all0),PFS_V (Pin (Port1,1),Bits8_all0),
    PFS_V (Pin (Port1,2),Bits8_all0),PFS_V (Pin (Port1,3),Bits8_all0),
    PFS_V (Pin (Port1,4),Bits8_all0),PFS_V (Pin (Port1,5),Bits8_all0),
    PFS_V (Pin (Port1,6),Bits8_all0),PFS_V (Pin (Port1,7),Bits8_all0),
    PFS_V (Pin (Port2,0),Bits8_all0),PFS_V (Pin (Port2,1),Bits8_all0),
    PFS_V (Pin (Port2,2),Bits8_all0),PFS_V (Pin (Port2,3),Bits8_all0),
    PFS_V (Pin (Port2,4),Bits8_all0),PFS_V (Pin (Port2,5),Bits8_all0),
    PFS_V (Pin (Port2,6),Bits8_all0),PFS_V (Pin (Port2,7),Bits8_all0),
    PFS_V (Pin (Port3,0),Bits8_all0),PFS_V (Pin (Port3,1),Bits8_all0),
    PFS_V (Pin (Port3,2),Bits8_all0),PFS_V (Pin (Port3,3),Bits8_all0),
    PFS_V (Pin (Port3,4),Bits8_all0),PFS_V (Pin (Port3,5),Bits8_all0),
    PFS_V (Pin (Port3,6),Bits8_all0),PFS_V (Pin (Port3,7),Bits8_all0),
    PFS_V (Pin (Port4,0),Bits8_all0),PFS_V (Pin (Port4,1),Bits8_all0),
    PFS_V (Pin (Port4,2),Bits8_all0),PFS_V (Pin (Port4,3),Bits8_all0),
    PFS_V (Pin (Port4,4),Bits8_all0),PFS_V (Pin (Port4,5),Bits8_all0),
    PFS_V (Pin (Port4,6),Bits8_all0),PFS_V (Pin (Port4,7),Bits8_all0),
    PFS_V (Pin (Port5,0),Bits8_all0),PFS_V (Pin (Port5,1),Bits8_all0),
    PFS_V (Pin (Port5,2),Bits8_all0),PFS_V (Pin (Port5,3),Bits8_all0),
    PFS_V (Pin (Port5,4),Bits8_all0),PFS_V (Pin (Port5,5),Bits8_all0),
    PFS_V (Pin (Port5,6),Bits8_all0),PFS_V (Pin (Port5,7),Bits8_all0),
    PFS_V (Pin (PortA,0),Bits8_all0),PFS_V (Pin (PortA,1),Bits8_all0),
    PFS_V (Pin (PortA,2),Bits8_all0),PFS_V (Pin (PortA,3),Bits8_all0),
    PFS_V (Pin (PortA,4),Bits8_all0),PFS_V (Pin (PortA,5),Bits8_all0),
    PFS_V (Pin (PortA,6),Bits8_all0),PFS_V (Pin (PortA,7),Bits8_all0),
    PFS_V (Pin (PortB,0),Bits8_all0),PFS_V (Pin (PortB,1),Bits8_all0),
    PFS_V (Pin (PortB,2),Bits8_all0),PFS_V (Pin (PortB,3),Bits8_all0),
    PFS_V (Pin (PortB,4),Bits8_all0),PFS_V (Pin (PortB,5),Bits8_all0),
    PFS_V (Pin (PortB,6),Bits8_all0),PFS_V (Pin (PortB,7),Bits8_all0),
    PFS_V (Pin (PortC,0),Bits8_all0),PFS_V (Pin (PortC,1),Bits8_all0),
    PFS_V (Pin (PortC,2),Bits8_all0),PFS_V (Pin (PortC,3),Bits8_all0),
    PFS_V (Pin (PortC,4),Bits8_all0),PFS_V (Pin (PortC,5),Bits8_all0),
    PFS_V (Pin (PortC,6),Bits8_all0),PFS_V (Pin (PortC,7),Bits8_all0),
    PFS_V (Pin (PortE,0),Bits8_all0),PFS_V (Pin (PortE,1),Bits8_all0),
    PFS_V (Pin (PortE,2),Bits8_all0),PFS_V (Pin (PortE,3),Bits8_all0),
    PFS_V (Pin (PortE,4),Bits8_all0),PFS_V (Pin (PortE,5),Bits8_all0),
    PFS_V (Pin (PortE,6),Bits8_all0),PFS_V (Pin (PortE,7),Bits8_all0),
    PFS_V (Pin (PortH,0),Bits8_all0),PFS_V (Pin (PortH,1),Bits8_all0),
    PFS_V (Pin (PortH,2),Bits8_all0),PFS_V (Pin (PortH,3),Bits8_all0),
    PFS_V (Pin (PortH,4),Bits8_all0),PFS_V (Pin (PortH,5),Bits8_all0),
    PFS_V (Pin (PortH,6),Bits8_all0),PFS_V (Pin (PortH,7),Bits8_all0),
    PFS_V (Pin (PortJ,0),Bits8_all0),PFS_V (Pin (PortJ,1),Bits8_all0),
    PFS_V (Pin (PortJ,2),Bits8_all0),PFS_V (Pin (PortJ,3),Bits8_all0),
    PFS_V (Pin (PortJ,4),Bits8_all0),PFS_V (Pin (PortJ,5),Bits8_all0),
    PFS_V (Pin (PortJ,6),Bits8_all0),PFS_V (Pin (PortJ,7),Bits8_all0)
   )

praxi rx110_consume_pfs_views (
    PFS_V (Pin (Port0,0),Bits8_all0),PFS_V (Pin (Port0,1),Bits8_all0),
    PFS_V (Pin (Port0,2),Bits8_all0),PFS_V (Pin (Port0,3),Bits8_all0),
    PFS_V (Pin (Port0,4),Bits8_all0),PFS_V (Pin (Port0,5),Bits8_all0),
    PFS_V (Pin (Port0,6),Bits8_all0),PFS_V (Pin (Port0,7),Bits8_all0),
    PFS_V (Pin (Port1,0),Bits8_all0),PFS_V (Pin (Port1,1),Bits8_all0),
    PFS_V (Pin (Port1,2),Bits8_all0),PFS_V (Pin (Port1,3),Bits8_all0),
    PFS_V (Pin (Port1,4),Bits8_all0),PFS_V (Pin (Port1,5),Bits8_all0),
    PFS_V (Pin (Port1,6),Bits8_all0),PFS_V (Pin (Port1,7),Bits8_all0),
    PFS_V (Pin (Port2,0),Bits8_all0),PFS_V (Pin (Port2,1),Bits8_all0),
    PFS_V (Pin (Port2,2),Bits8_all0),PFS_V (Pin (Port2,3),Bits8_all0),
    PFS_V (Pin (Port2,4),Bits8_all0),PFS_V (Pin (Port2,5),Bits8_all0),
    PFS_V (Pin (Port2,6),Bits8_all0),PFS_V (Pin (Port2,7),Bits8_all0),
    PFS_V (Pin (Port3,0),Bits8_all0),PFS_V (Pin (Port3,1),Bits8_all0),
    PFS_V (Pin (Port3,2),Bits8_all0),PFS_V (Pin (Port3,3),Bits8_all0),
    PFS_V (Pin (Port3,4),Bits8_all0),PFS_V (Pin (Port3,5),Bits8_all0),
    PFS_V (Pin (Port3,6),Bits8_all0),PFS_V (Pin (Port3,7),Bits8_all0),
    PFS_V (Pin (Port4,0),Bits8_all0),PFS_V (Pin (Port4,1),Bits8_all0),
    PFS_V (Pin (Port4,2),Bits8_all0),PFS_V (Pin (Port4,3),Bits8_all0),
    PFS_V (Pin (Port4,4),Bits8_all0),PFS_V (Pin (Port4,5),Bits8_all0),
    PFS_V (Pin (Port4,6),Bits8_all0),PFS_V (Pin (Port4,7),Bits8_all0),
    PFS_V (Pin (Port5,0),Bits8_all0),PFS_V (Pin (Port5,1),Bits8_all0),
    PFS_V (Pin (Port5,2),Bits8_all0),PFS_V (Pin (Port5,3),Bits8_all0),
    PFS_V (Pin (Port5,4),Bits8_all0),PFS_V (Pin (Port5,5),Bits8_all0),
    PFS_V (Pin (Port5,6),Bits8_all0),PFS_V (Pin (Port5,7),Bits8_all0),
    PFS_V (Pin (PortA,0),Bits8_all0),PFS_V (Pin (PortA,1),Bits8_all0),
    PFS_V (Pin (PortA,2),Bits8_all0),PFS_V (Pin (PortA,3),Bits8_all0),
    PFS_V (Pin (PortA,4),Bits8_all0),PFS_V (Pin (PortA,5),Bits8_all0),
    PFS_V (Pin (PortA,6),Bits8_all0),PFS_V (Pin (PortA,7),Bits8_all0),
    PFS_V (Pin (PortB,0),Bits8_all0),PFS_V (Pin (PortB,1),Bits8_all0),
    PFS_V (Pin (PortB,2),Bits8_all0),PFS_V (Pin (PortB,3),Bits8_all0),
    PFS_V (Pin (PortB,4),Bits8_all0),PFS_V (Pin (PortB,5),Bits8_all0),
    PFS_V (Pin (PortB,6),Bits8_all0),PFS_V (Pin (PortB,7),Bits8_all0),
    PFS_V (Pin (PortC,0),Bits8_all0),PFS_V (Pin (PortC,1),Bits8_all0),
    PFS_V (Pin (PortC,2),Bits8_all0),PFS_V (Pin (PortC,3),Bits8_all0),
    PFS_V (Pin (PortC,4),Bits8_all0),PFS_V (Pin (PortC,5),Bits8_all0),
    PFS_V (Pin (PortC,6),Bits8_all0),PFS_V (Pin (PortC,7),Bits8_all0),
    PFS_V (Pin (PortE,0),Bits8_all0),PFS_V (Pin (PortE,1),Bits8_all0),
    PFS_V (Pin (PortE,2),Bits8_all0),PFS_V (Pin (PortE,3),Bits8_all0),
    PFS_V (Pin (PortE,4),Bits8_all0),PFS_V (Pin (PortE,5),Bits8_all0),
    PFS_V (Pin (PortE,6),Bits8_all0),PFS_V (Pin (PortE,7),Bits8_all0),
    PFS_V (Pin (PortH,0),Bits8_all0),PFS_V (Pin (PortH,1),Bits8_all0),
    PFS_V (Pin (PortH,2),Bits8_all0),PFS_V (Pin (PortH,3),Bits8_all0),
    PFS_V (Pin (PortH,4),Bits8_all0),PFS_V (Pin (PortH,5),Bits8_all0),
    PFS_V (Pin (PortH,6),Bits8_all0),PFS_V (Pin (PortH,7),Bits8_all0),
    PFS_V (Pin (PortJ,0),Bits8_all0),PFS_V (Pin (PortJ,1),Bits8_all0),
    PFS_V (Pin (PortJ,2),Bits8_all0),PFS_V (Pin (PortJ,3),Bits8_all0),
    PFS_V (Pin (PortJ,4),Bits8_all0),PFS_V (Pin (PortJ,5),Bits8_all0),
    PFS_V (Pin (PortJ,6),Bits8_all0),PFS_V (Pin (PortJ,7),Bits8_all0)
   ): void

praxi rx110_initial_pdr_views ():
   (PDR_V (Port0,Bits8_all0),PDR_V (Port1,Bits8_all0),
    PDR_V (Port2,Bits8_all0),PDR_V (Port3,Bits8_all0),
    PDR_V (Port4,Bits8_all0),PDR_V (Port5,Bits8_all0),
    PDR_V (PortA,Bits8_all0),PDR_V (PortB,Bits8_all0),
    PDR_V (PortC,Bits8_all0),PDR_V (PortE,Bits8_all0),
    PDR_V (PortH,Bits8_all0),PDR_V (PortJ,Bits8_all0),
    PDR_PERMISSION (Port0,BitPerms8_all), PDR_PERMISSION (Port1,BitPerms8_all),
    PDR_PERMISSION (Port2,BitPerms8_all), PDR_PERMISSION (Port3,BitPerms8_all),
    PDR_PERMISSION (Port4,BitPerms8_all), PDR_PERMISSION (Port5,BitPerms8_all),
    PDR_PERMISSION (PortA,BitPerms8_all), PDR_PERMISSION (PortB,BitPerms8_all),
    PDR_PERMISSION (PortC,BitPerms8_all), PDR_PERMISSION (PortE,BitPerms8_all),
    PDR_PERMISSION (PortH,BitPerms8_all), PDR_PERMISSION (PortJ,BitPerms8_all)
   )

praxi rx110_consume_pdr_views (
    PDR_V (Port0,Bits8_all0),PDR_V (Port1,Bits8_all0),
    PDR_V (Port2,Bits8_all0),PDR_V (Port3,Bits8_all0),
    PDR_V (Port4,Bits8_all0),PDR_V (Port5,Bits8_all0),
    PDR_V (PortA,Bits8_all0),PDR_V (PortB,Bits8_all0),
    PDR_V (PortC,Bits8_all0),PDR_V (PortE,Bits8_all0),
    PDR_V (PortH,Bits8_all0),PDR_V (PortJ,Bits8_all0),
    PDR_PERMISSION (Port0,BitPerms8_all), PDR_PERMISSION (Port1,BitPerms8_all),
    PDR_PERMISSION (Port2,BitPerms8_all), PDR_PERMISSION (Port3,BitPerms8_all),
    PDR_PERMISSION (Port4,BitPerms8_all), PDR_PERMISSION (Port5,BitPerms8_all),
    PDR_PERMISSION (PortA,BitPerms8_all), PDR_PERMISSION (PortB,BitPerms8_all),
    PDR_PERMISSION (PortC,BitPerms8_all), PDR_PERMISSION (PortE,BitPerms8_all),
    PDR_PERMISSION (PortH,BitPerms8_all), PDR_PERMISSION (PortJ,BitPerms8_all)
   ): void

praxi rx110_initial_podr_views ():
   (PODR_V (Port0,Bits8_all0),PODR_V (Port1,Bits8_all0),
    PODR_V (Port2,Bits8_all0),PODR_V (Port3,Bits8_all0),
    PODR_V (Port4,Bits8_all0),PODR_V (Port5,Bits8_all0),
    PODR_V (PortA,Bits8_all0),PODR_V (PortB,Bits8_all0),
    PODR_V (PortC,Bits8_all0),PODR_V (PortE,Bits8_all0),
    PODR_V (PortH,Bits8_all0),PODR_V (PortJ,Bits8_all0),
    PODR_PERMISSION (Port0,BitPerms8_all), PODR_PERMISSION (Port1,BitPerms8_all),
    PODR_PERMISSION (Port2,BitPerms8_all), PODR_PERMISSION (Port3,BitPerms8_all),
    PODR_PERMISSION (Port4,BitPerms8_all), PODR_PERMISSION (Port5,BitPerms8_all),
    PODR_PERMISSION (PortA,BitPerms8_all), PODR_PERMISSION (PortB,BitPerms8_all),
    PODR_PERMISSION (PortC,BitPerms8_all), PODR_PERMISSION (PortE,BitPerms8_all),
    PODR_PERMISSION (PortH,BitPerms8_all), PODR_PERMISSION (PortJ,BitPerms8_all)
   )

praxi rx110_consume_podr_views (
    PODR_V (Port0,Bits8_all0),PODR_V (Port1,Bits8_all0),
    PODR_V (Port2,Bits8_all0),PODR_V (Port3,Bits8_all0),
    PODR_V (Port4,Bits8_all0),PODR_V (Port5,Bits8_all0),
    PODR_V (PortA,Bits8_all0),PODR_V (PortB,Bits8_all0),
    PODR_V (PortC,Bits8_all0),PODR_V (PortE,Bits8_all0),
    PODR_V (PortH,Bits8_all0),PODR_V (PortJ,Bits8_all0),
    PODR_PERMISSION (Port0,BitPerms8_all), PODR_PERMISSION (Port1,BitPerms8_all),
    PODR_PERMISSION (Port2,BitPerms8_all), PODR_PERMISSION (Port3,BitPerms8_all),
    PODR_PERMISSION (Port4,BitPerms8_all), PODR_PERMISSION (Port5,BitPerms8_all),
    PODR_PERMISSION (PortA,BitPerms8_all), PODR_PERMISSION (PortB,BitPerms8_all),
    PODR_PERMISSION (PortC,BitPerms8_all), PODR_PERMISSION (PortE,BitPerms8_all),
    PODR_PERMISSION (PortH,BitPerms8_all), PODR_PERMISSION (PortJ,BitPerms8_all)
   ): void

#endif
#endif


Small fix in parsing command line arguments

(**

*)
#include
"share/atspre_define.hats"
#include
"share/atspre_staload.hats"

(* ****** ****** *)

staload "patsolve.sats"

(* ****** ****** *)

local

#define :: list_cons
#define nil list_nil

fun
fail {n:int | n >=1}  (
  argv: !argv(n)
): commarglst = let
  val () = prerrln!
    ("Usage: ", argv[0], " [-s <script>] [-v --verbose]")
in
  exit (1)
end

in

implement
parse_argv {n} (argc, argv) = let
  fun loop {i:int | i >=1; i <= n} (
    i: int i, argv: !argv (n), res: commarglst
  ): commarglst =
    if i = argc then
      res
    else let
      val cmd = argv[i]
    in
      case+ 0 of
        | _ when cmd = "-v" orelse cmd = "--verbose" =>
          loop (succ(i), argv, Verbose() :: res)
        | _ when cmd = "-s" => let
          val i = succ (i)
        in
          if i < argc then let
            val s = argv[i]
          in
            loop (succ (i), argv, Script (s) :: res)
          end
          else
            fail (argv)
        end
        | _ =>>
          fail (argv)
    end
in
  loop (1, argv, nil())
end

end // end of [local]

(**

*)
#include
"share/atspre_define.hats"
#include
"share/atspre_staload.hats"

(* ****** ****** *)

staload "patsolve.sats"

(* ****** ****** *)

local

#define :: list_cons
#define nil list_nil

fun
fail {n:int | n >=1}  (
  argv: !argv(n)
): commarglst = let
  val () = prerrln!
    ("Usage: ", argv[0], " [-s <script>] [-v --verbose]")
in
  exit (1)
end

in

implement
parse_argv {n} (argc, argv) = let
  fun loop {i:int | i >=1; i <= n} (
    i: int i, argv: !argv (n), res: commarglst
  ): commarglst =
    if i = argc then
      res
    else let
      val cmd = argv[i]
    in
      case+ 0 of
        | _ when cmd = "-v" orelse cmd = "--verbose" =>
          loop (succ(i), argv, Verbose() :: res)
        | _ when cmd = "-s" => let
          val i = succ (i)
        in
          if i < argc then let
            val s = argv[i]
          in
            loop (i, argv, Script (s) :: res)
          end
          else
            fail (argv)
        end
        | _ =>
          fail (argv)
    end
in
  loop (1, argv, nil())
end

end // end of [local]


float values in ATS

float values in ATS

// This is a standard function-pack
// strongly recommend to include it everywhere
// to avoid very long-time debug sessions.

#include "share/atspre_staload.hats"

val _ = println! ("(1+2)/(3+4) = ", (1+2)/(3+4))

val _ = println! ("-1 = ", x) where {
    val x = ~1
}

val _ = (
    println! ("0.123 = ", 0.123);
    println! ("0.000314 = ", 314E-6);
)

implement main0 () = ()

// This is a standard function-pack
// strongly recommend to include it everywhere
// to avoid very long-time debug sessions.

#include "share/atspre_staload.hats"

val _ = println! ("(1+2)/(3+4) = ", (1+2)/(3+4))

val _ = println! ("-1 = ", x) where {
    val x = ~1
}

implement main0 () = ()


Use println! directly.

Use println! directly.

#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"

staload UN = "prelude/SATS/unsafe.sats"
staload "libmbedats/SATS/mbed_api_wait_api.sats"
staload "libmbedats/SATS/mbed_hal_gpio_api.sats"
staload "libmbedats/SATS/mbed_hal_serial_api.sats"

#define BLINK_DELAY_US 500000

%{
gpio_t c_led1;
gpio_t c_led2;
gpio_t c_led3;
gpio_t c_led4;
serial_t c_serial;
%}

macdef led1 = $extval(gpio_t_p, "(&c_led1)")
macdef led2 = $extval(gpio_t_p, "(&c_led2)")
macdef led3 = $extval(gpio_t_p, "(&c_led3)")
macdef led4 = $extval(gpio_t_p, "(&c_led4)")
macdef serial = $extval(serial_t_p, "(&c_serial)")

fun loop (): void = {
  val () = gpio_write (led1, 1)
  val () = gpio_write (led2, 0)
  val () = gpio_write (led3, 1)
  val () = gpio_write (led4, 0)
  val () = println! ("A", 1, "\r")
  val () = wait_us (BLINK_DELAY_US);
  val () = gpio_write (led1, 0)
  val () = gpio_write (led2, 1)
  val () = gpio_write (led3, 0)
  val () = gpio_write (led4, 1)
  val () = println! ("B", 2, "\r")
  val () = wait_us (BLINK_DELAY_US);
  val () = loop ();
}

implement main0 () = {
  val () = gpio_init_out (led1, LED1)
  val () = gpio_init_out (led2, LED2)
  val () = gpio_init_out (led3, LED3)
  val () = gpio_init_out (led4, LED4)
  val () = println! ("Hello world!\r")
  val () = loop ();
}

#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"

staload UN = "prelude/SATS/unsafe.sats"
staload "libmbedats/SATS/mbed_api_wait_api.sats"
staload "libmbedats/SATS/mbed_hal_gpio_api.sats"
staload "libmbedats/SATS/mbed_hal_serial_api.sats"

#define BLINK_DELAY_US 500000

%{
gpio_t c_led1;
gpio_t c_led2;
gpio_t c_led3;
gpio_t c_led4;
serial_t c_serial;
%}

macdef led1 = $extval(gpio_t_p, "(&c_led1)")
macdef led2 = $extval(gpio_t_p, "(&c_led2)")
macdef led3 = $extval(gpio_t_p, "(&c_led3)")
macdef led4 = $extval(gpio_t_p, "(&c_led4)")
macdef serial = $extval(serial_t_p, "(&c_serial)")

fun loop (): void = {
  val () = gpio_write (led1, 1)
  val () = gpio_write (led2, 0)
  val () = gpio_write (led3, 1)
  val () = gpio_write (led4, 0)
  val () = serial_putc (serial, char2i 'A')
  val () = wait_us (BLINK_DELAY_US);
  val () = gpio_write (led1, 0)
  val () = gpio_write (led2, 1)
  val () = gpio_write (led3, 0)
  val () = gpio_write (led4, 1)
  val () = serial_putc (serial, char2i 'B')
  val () = wait_us (BLINK_DELAY_US);
  val () = loop ();
}

implement main0 () = {
  val () = gpio_init_out (led1, LED1)
  val () = gpio_init_out (led2, LED2)
  val () = gpio_init_out (led3, LED3)
  val () = gpio_init_out (led4, LED4)
  val () = serial_init (serial, USBTX, USBRX)
  val () = serial_baud (serial, 9600)
  val () = serial_putc (serial, char2i 'I')
  val () = loop ();
}


Updating: improving LinuxKerneling/chardev-1

(*
** A simple char device
*)

(* ****** ****** *)
//
// HX-2014-10-24:
// nothing fancy here;
// just trying to get it working
//
(* ****** ****** *)
//
// HX-2014-10-21:
// no use for dynloading
//
#define ATS_DYNLOADFLAG 0
//
(* ****** ****** *)
//
#include
"share/atspre_define.hats"
//
#include
"{$KERNELATS}/prelude/staloadall.hats"
//
(* ****** ****** *)
//
staload
"{$LINUX}/basics.sats"
//
(* ****** ****** *)
//
staload
"{$LINUX}/SATS/errno.sats"
staload
"{$LINUX}/SATS/types.sats"
//
(* ****** ****** *)
//
staload
"{$LINUX}/SATS/init.sats"
staload
"{$LINUX}/SATS/module.sats"
staload
"{$LINUX}/SATS/kernel.sats"
staload $PRINTK // HX: opening NS
//
staload "{$LINUX}/SATS/fs.sats"
//
staload "{$LINUX}/SATS/asm/uaccess.sats"
//
(* ****** ****** *)

staload
UN = "prelude/SATS/unsafe.sats"

(* ****** ****** *)

%{^
MODULE_LICENSE("Dual BSD/GPL") ;
%} // end of [%{^]

(* ****** ****** *)

static
fun init_module(): interr = "mac#"
static
fun cleanup_module(): void = "mac#"

(* ****** ****** *)

%{^
static int Major = 0;
static int Device_open = 0;
static int Device_open_count = 0;
%} // end of [%{^]

macdef
Major = $extval(int, "Major")
macdef
Device_open = $extval(int, "Device_open")
macdef
Device_open_count = $extval(int, "Device_open_count")

(* ****** ****** *)

%{^
#define BUFLEN 80 // Max length of the message from the device
#define DEVICE_NAME "chardev-1" /* Dev name as it appears in /proc/devices */
//
typedef
char *charptr ;
char Message[BUFLEN];
charptr Message0_ptr = &Message[0];
charptr Message1_ptr = &Message[0];
//
%} // end of [%{^]

typedef charptr = $extype"charptr"

macdef
DEVICE_NAME = $extval(string, "DEVICE_NAME")
macdef
Message0_ptr = $extval(charptr, "Message0_ptr")
macdef
Message1_ptr = $extval(charptr, "Message1_ptr")

(* ****** ****** *)

%{^
//
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
//
%} // end of [%{^]

(* ****** ****** *)

%{^

struct
file_operations
fops = {
  .read = device_read,
  .write = device_write,
  .open = device_open,
  .release = device_release
} ;

static void *fops_ptr = &(fops);

%} // end of [%{^]

macdef
fops_ptr = $extval(fops_ptr1, "fops_ptr")

(* ****** ****** *)

implement
init_module
(
) = let
//
extvar
"Major" =
register_chrdev(0u, DEVICE_NAME, fops_ptr)
//
val Major = g1ofg0(Major)
//
in
//
if
(Major > 0)
then let
//
val () =
$extfcall
(
  void, "printk"
, KERN_INFO_"chardev-1: assigned major number = %d\n", Major
) (* end of [val] *)
//
in
  0(*SUCCESS*)
end // end of [then]
else let
//
val () =
$extfcall
(
  void, "printk"
, KERN_ALERT_"chardev-1: registering failed with %d\n", Major
) (* end of [val] *)
//
in
  Major
end // end of [else]
//
end (* end of [init_module] *)

(* ****** ****** *)

implement
cleanup_module
(
// argumentless
) = () where
{
//
val
Major =
(
if Major > 0
  then g0i2u(Major) else 0u
// end of [if]
) : uint // end of [val]
//
val () = unregister_chrdev(Major, DEVICE_NAME)
//
val () =
$extfcall
(
  void, "printk"
, KERN_ALERT_"chardev-1: it has been opened for %d times\n"
, Device_open_count
) (* end of [val] *)
//
} (* end of [cleanup_module] *)

(* ****** ****** *)
//
static
fun device_open_ (inode_ptr1, file_ptr1): interr = "mac#"
static
fun device_release_ (inode_ptr1, file_ptr1): interr = "mac#"
//
(* ****** ****** *)

implement
device_open_
  (inx, filp) = let
in
//
if
(Device_open = 0)
then let
//
extvar
"Device_open" = Device_open + 1
extvar
"Device_open_count" = Device_open_count + 1
//
val () =
$extfcall
(
  void
, "sprintf"
, Message0_ptr, "Hello(%d) from the chardev\n", Device_open_count
) : void // end of [val]
//
extvar
"Message1_ptr" = Message0_ptr
//
in
  0(*SUCCESS*)
end // end of [then]
else ~(EBUSY) // end of [else]
//
end // end of [device_open_]

(* ****** ****** *)

implement
device_release_
  (inx, filp) = let
//
extvar
"Device_open" = Device_open - 1
//
in
  0(*SUCCESS*)
end // end of [device_release_]

(* ****** ****** *)

%{$
//
static
inline
int
device_open
(
struct inode *p0, struct file *p1
) { return device_open_(p0, p1) ; }
//
static
inline
int
device_release
(
struct inode *p0, struct file *p1
) { return device_release_(p0, p1) ; }
//
%} // end of [%{$]

(* ****** ****** *)
//
static
fun
device_read_{n:int}
(
  file_ptr1, buf: &ubytes(n), size_t(n), ofs: &loff_t
) : ssize_t = "mac#" // end-of-function
static
fun
device_write_{n:int}
(
  file_ptr1, buf: &ubytes(n), size_t(n), ofs: &loff_t
) : ssize_t = "mac#" // end-of-function
//
(* ****** ****** *)

implement
device_read_
  (filp, buf, n, ofs) = let
//
val n2 =
string_nlength<>
  ($UN.cast{string}(Message1_ptr), n)
//
val
n2_ =
$extfcall
(
  size_t
, "copy_to_user", addr@buf, Message1_ptr, n2
) (* end of [val] *)
//
val nread = n2 - n2_
//
extvar
"Message1_ptr" =
add_ptr_bsz ($UN.cast{ptr}(Message1_ptr), nread)
//
in
  $UN.cast{ssize_t}(nread)
end // end of [device_read_]

(* ****** ****** *)

implement
device_write_
  (filp, buf, n, ofs) = let
//
val () =
$extfcall
(
  void, "printk"
, KERN_ALERT_"chardev-1: this operation isn't supported.\n"
) (* end of [val] *)
//
in
  $UN.cast{ssize_t}(~(EINVAL))
end // end of [device_write_]

(* ****** ****** *)

%{$
//
static
inline
ssize_t
device_read
(
struct file *p0, char *buf, size_t n, loff_t *ofs
) { return device_read_(p0, buf, n, ofs) ; }
//
static
inline
ssize_t
device_write
(
struct file *p0, const char *buf, size_t n, loff_t *ofs
) { return device_write_(p0, (void*)buf, n, ofs) ; }
//
%} // end of [%{$]

(* ****** ****** *)

(* end of [chardev-1.dats] *)

(*
** A simple char device
*)

(* ****** ****** *)
//
// HX-2014-10-24:
// nothing fancy here;
// just trying to get it working
//
(* ****** ****** *)
//
// HX-2014-10-21:
// no use for dynloading
//
#define ATS_DYNLOADFLAG 0
//
(* ****** ****** *)
//
#include
"share/atspre_define.hats"
//
#include
"{$KERNELATS}/prelude/staloadall.hats"
//
(* ****** ****** *)
//
staload
"{$LINUX}/basics.sats"
//
(* ****** ****** *)
//
staload
"{$LINUX}/SATS/errno.sats"
staload
"{$LINUX}/SATS/types.sats"
//
(* ****** ****** *)
//
staload
"{$LINUX}/SATS/init.sats"
staload
"{$LINUX}/SATS/module.sats"
staload
"{$LINUX}/SATS/kernel.sats"
staload $PRINTK // HX: opening NS
//
staload "{$LINUX}/SATS/fs.sats"
//
staload "{$LINUX}/SATS/asm/uaccess.sats"
//
(* ****** ****** *)

staload
UN = "prelude/SATS/unsafe.sats"

(* ****** ****** *)

%{^
MODULE_LICENSE("Dual BSD/GPL") ;
%} // end of [%{^]

(* ****** ****** *)

static
fun init_module(): interr = "mac#"
static
fun cleanup_module(): void = "mac#"

(* ****** ****** *)

%{^
static int Major = 0;
static int Device_open = 0;
static int Device_open_count = 0;
%} // end of [%{^]

macdef
Major = $extval(int, "Major")
macdef
Device_open = $extval(int, "Device_open")
macdef
Device_open_count = $extval(int, "Device_open_count")

(* ****** ****** *)

%{^
#define BUFLEN 80 // Max length of the message from the device
#define DEVICE_NAME "chardev-1" /* Dev name as it appears in /proc/devices */
//
typedef
char *charptr ;
char Message[BUFLEN];
charptr Message0_ptr = &Message[0];
charptr Message1_ptr = &Message[0];
//
%} // end of [%{^]

typedef charptr = $extype"charptr"

macdef
DEVICE_NAME = $extval(string, "DEVICE_NAME")
macdef
Message0_ptr = $extval(charptr, "Message0_ptr")
macdef
Message1_ptr = $extval(charptr, "Message1_ptr")

(* ****** ****** *)

%{^
//
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
//
%} // end of [%{^]

(* ****** ****** *)

%{^

struct
file_operations
fops = {
  .read = device_read,
  .write = device_write,
  .open = device_open,
  .release = device_release
} ;

static void *fops_ptr = &(fops);

%} // end of [%{^]

macdef
fops_ptr = $extval(fops_ptr1, "fops_ptr")

(* ****** ****** *)

implement
init_module
(
) = let
//
extvar
"Major" =
register_chrdev(0u, DEVICE_NAME, fops_ptr)
//
val Major = g1ofg0(Major)
//
in
//
if
(Major > 0)
then let
//
val () =
$extfcall
(
  void
, "printk", KERN_INFO_"The assigned major number is: %d\n", Major
) (* end of [val] *)
//
in
  0(*SUCCESS*)
end // end of [then]
else let
//
val () =
$extfcall
(
  void
, "printk", KERN_ALERT_"Registering chardev failed with %d\n", Major
) (* end of [val] *)
//
in
  Major
end // end of [else]
//
end (* end of [init_module] *)

(* ****** ****** *)

implement
cleanup_module
(
// argumentless
) = () where
{
//
val
Major =
(
if Major > 0
  then g0i2u(Major) else 0u
// end of [if]
) : uint // end of [val]
//
val () = unregister_chrdev(Major, DEVICE_NAME)
//
} (* end of [cleanup_module] *)

(* ****** ****** *)
//
static
fun device_open_ (inode_ptr1, file_ptr1): interr = "mac#"
static
fun device_release_ (inode_ptr1, file_ptr1): interr = "mac#"
//
(* ****** ****** *)

implement
device_open_
  (inx, filp) = let
in
//
if
(Device_open = 0)
then let
//
extvar
"Device_open" = Device_open + 1
extvar
"Device_open_count" = Device_open_count + 1
//
val () =
$extfcall
(
  void
, "sprintf"
, Message0_ptr, "Hello(%d) from the chardev\n", Device_open_count
) : void // end of [val]
//
extvar
"Message1_ptr" = Message0_ptr
//
in
  0(*SUCCESS*)
end // end of [then]
else ~(EBUSY) // end of [else]
//
end // end of [device_open_]

(* ****** ****** *)

implement
device_release_
  (inx, filp) = let
//
extvar
"Device_open" = Device_open - 1
//
in
  0(*SUCCESS*)
end // end of [device_release_]

(* ****** ****** *)

%{$
//
static
inline
int
device_open
(
struct inode *p0, struct file *p1
) { return device_open_(p0, p1) ; }
//
static
inline
int
device_release
(
struct inode *p0, struct file *p1
) { return device_release_(p0, p1) ; }
//
%} // end of [%{$]

(* ****** ****** *)
//
static
fun
device_read_{n:int}
(
  file_ptr1, buf: &ubytes(n), size_t(n), ofs: &loff_t
) : ssize_t = "mac#" // end-of-function
static
fun
device_write_{n:int}
(
  file_ptr1, buf: &ubytes(n), size_t(n), ofs: &loff_t
) : ssize_t = "mac#" // end-of-function
//
(* ****** ****** *)

implement
device_read_
  (filp, buf, n, ofs) = let
//
val n2 =
string_nlength<>
  ($UN.cast{string}(Message1_ptr), n)
//
val
n2_ =
$extfcall
(
  size_t
, "copy_to_user", addr@buf, Message1_ptr, n2
) (* end of [val] *)
//
val nread = n2 - n2_
//
extvar
"Message1_ptr" =
add_ptr_bsz ($UN.cast{ptr}(Message1_ptr), nread)
//
in
  $UN.cast{ssize_t}(nread)
end // end of [device_read_]

(* ****** ****** *)

implement
device_write_
  (filp, buf, n, ofs) = let
//
val () =
$extfcall
(
  void
, "printk"
, KERN_ALERT_"Sorry, this operation isn't supported.\n"
) (* end of [val] *)
//
in
  $UN.cast{ssize_t}(~(EINVAL))
end // end of [device_write_]

(* ****** ****** *)

%{$
//
static
inline
ssize_t
device_read
(
struct file *p0, char *buf, size_t n, loff_t *ofs
) { return device_read_(p0, buf, n, ofs) ; }
//
static
inline
ssize_t
device_write
(
struct file *p0, const char *buf, size_t n, loff_t *ofs
) { return device_write_(p0, (void*)buf, n, ofs) ; }
//
%} // end of [%{$]

(* ****** ****** *)

(* end of [chardev-1.dats] *)


Use natLt

Use natLt

%{^
#include <avr/io.h>
#include <util/delay.h>
#include "Arduino.h"
%}

#define LOW 0x0
#define HIGH 0x1

#define INPUT 0x0
#define OUTPUT 0x1

#define LSBFIRST 0
#define MSBFIRST 1

typedef pin_t = natLt(256)

macdef LED_BUILTIN = $extval(pin_t, "LED_BUILTIN")
macdef A0          = $extval(pin_t, "A0")

abst@ype ANALOG_REFERENCE = $extype"uint8_t"
macdef DEFAULT = $extval(ANALOG_REFERENCE, "DEFAULT")
macdef INTERNAL = $extval(ANALOG_REFERENCE, "INTERNAL")
macdef EXTERNAL = $extval(ANALOG_REFERENCE, "EXTERNAL")

fun init (): void = "mac#"
fun pinMode (pin: pin_t, mode: natLt(2)): void = "mac#"
fun digitalWrite (pin: pin_t, value: natLt(2)): void = "mac#"
fun digitalRead (pin: pin_t): natLt(2) = "mac#"
fun analogReference (atype: ANALOG_REFERENCE): void = "mac#"
fun analogRead (pin: pin_t): natLt(1024) = "mac#"
fun analogWrite (pin: pin_t, value: natLt(256)): void = "mac#"
fun millis (): ulint = "mac#"
fun micros (): ulint = "mac#"
fun delay_ms (ms: double): void = "mac#_delay_ms"
fun delay_us (us: double): void = "mac#_delay_us"
fun interrupts (): void = "mac#"
fun nointerrupts (): void = "mac#"

fun main (): void (* Entry point for application *)

%{^
#include <avr/io.h>
#include <util/delay.h>
#include "Arduino.h"
%}

#define LOW 0x0
#define HIGH 0x1

#define INPUT 0x0
#define OUTPUT 0x1

#define LSBFIRST 0
#define MSBFIRST 1

typedef pin_t = natLt(256)

macdef LED_BUILTIN = $extval(pin_t, "LED_BUILTIN")
macdef A0          = $extval(pin_t, "A0")

abst@ype ANALOG_REFERENCE = $extype"uint8_t"
macdef DEFAULT = $extval(ANALOG_REFERENCE, "DEFAULT")
macdef INTERNAL = $extval(ANALOG_REFERENCE, "INTERNAL")
macdef EXTERNAL = $extval(ANALOG_REFERENCE, "EXTERNAL")

fun init (): void = "mac#"
fun pinMode (pin: pin_t, mode: natLt(2)): void = "mac#"
fun digitalWrite (pin: pin_t, value: natLt(2)): void = "mac#"
fun digitalRead (pin: pin_t): natLt(2) = "mac#"
fun analogReference (atype: ANALOG_REFERENCE): void = "mac#"
fun analogRead (pin: pin_t): [n:nat | n <= 1023] int n = "mac#"
fun analogWrite {n:nat | n <= 255} (pin: pin_t, value: int n): void = "mac#"
fun millis (): ulint = "mac#"
fun micros (): ulint = "mac#"
fun delay_ms (ms: double): void = "mac#_delay_ms"
fun delay_us (us: double): void = "mac#_delay_us"
fun interrupts (): void = "mac#"
fun nointerrupts (): void = "mac#"

fun main (): void (* Entry point for application *)


Snatch tcp_socket_connection_send_all

Snatch tcp_socket_connection_send_all

#include "share/atspre_staload.hats"

staload UN = "prelude/SATS/unsafe.sats"
staload "libmbedats/SATS/net_lwip_Socket_Socket.sats"
staload "libmbedats/SATS/net_lwip_Socket_Endpoint.sats"
staload "libmbedats/SATS/net_lwip_Socket_TCPSocketConnection.sats"

vtypedef TCPSocketConnection_struct = @{
  is_connected= bool
, sock=         Socket
, endpoint=     Endpoint
}

absvtype TCPSocketConnection_minus_struct (l:addr)

extern castfn
TCPSocketConnection_takeout_struct
(
  tcp: !TCPSocketConnection >> TCPSocketConnection_minus_struct l
) : #[l:addr] (TCPSocketConnection_struct @ l | ptr l)

extern praxi
TCPSocketConnection_addback_struct
  {l:addr}
(
  pfat: TCPSocketConnection_struct @ l
| tcp: !TCPSocketConnection_minus_struct l >> TCPSocketConnection
) : void

implement
tcp_socket_connection_open () = let
  val (pfgc, pfat | p) = ptr_alloc<TCPSocketConnection_struct> ()
  val () = p->is_connected := false
  val () = p->sock := socket_open ()
  val () = p->endpoint := endpoint_open ()
in
  $UN.castvwtp0{TCPSocketConnection}((pfat, pfgc | p))
end

implement
tcp_socket_connection_is_connected (tcp) = let
  val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
  val r = p->is_connected
  prval () = TCPSocketConnection_addback_struct(pfat | tcp)
in
  r
end

implement
tcp_socket_connection_connect (tcp, host, port) = let
  val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
  val rs = socket_initsock (p->sock, SOCK_STREAM)
  val ret = if rs = false then false else let
      val re = endpoint_set_address (p->endpoint, host, port)
    in
      if re = false then false else let
          abst@ype socklen_t = $extype"socklen_t"
          extern fun lwip_connect: (int, struct_sockaddr_in_p, socklen_t) -> int = "mac#"
          val fd = socket_sock_fd (p->sock)
          val rh = endpoint_remoteHost (p->endpoint)
          val rc = lwip_connect (fd, rh, $UN.cast (sizeof<struct_sockaddr_in>))
        in
          if rc < 0 then let
              val _ = socket_finisock (p->sock, true)
            in
              false
            end else let
              val () = p->is_connected := true
            in
              true
            end
        end
    end
  prval () = TCPSocketConnection_addback_struct(pfat | tcp)
in
  ret
end

implement
tcp_socket_connection_send_all (tcp, str) = let
  fun loop (tcp: !TCPSocketConnection, str: !strptr, fd: int, written: int): Option_vt int = let
      val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
      val blocking = socket_blocking (p->sock)
      prval () = TCPSocketConnection_addback_struct(pfat | tcp)
      val b = if blocking then true else let
                val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
                val tout = socket_timeout (p->sock)
                val r = socket_wait_writable (p->sock, tout)
                prval () = TCPSocketConnection_addback_struct(pfat | tcp)
              in
                r
              end
    in
      if b then let
          extern fun lwip_send: (int, ptr, int, int) -> int = "mac#"
          val p = strptr2ptr (str)
          val len = $UN.cast{int}(length (str))
          val r = lwip_send (fd, ptr_add<char>(p, written), len - written, 0)
        in
          if r < 0 then None_vt () else if r = 0 then let
              val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
              val () = p->is_connected := false
              prval () = TCPSocketConnection_addback_struct(pfat | tcp)
            in
              Some_vt (written)
            end
          else let
              val written2 = written + r
            in
              if written2 >= len then Some_vt (written2) else loop (tcp, str, fd, written2)
            end
        end
      else Some_vt (written)
    end
  val is_connected = tcp_socket_connection_is_connected (tcp)
  val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
  val fd = socket_sock_fd (p->sock)
  val blocking = socket_blocking (p->sock)
  prval () = TCPSocketConnection_addback_struct(pfat | tcp)
in
  if (fd < 0) orelse (not is_connected) then None_vt () else loop (tcp, str, fd, 0)
end

implement
tcp_socket_connection_receive (tcp) = let
  val is_connected = tcp_socket_connection_is_connected (tcp)
  val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
  val fd = socket_sock_fd (p->sock)
  val blocking = socket_blocking (p->sock)
  prval () = TCPSocketConnection_addback_struct(pfat | tcp)
in
  if (fd < 0) orelse (not is_connected) then None_vt () else let
      val b = if blocking then true else let
                val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
                val tout = socket_timeout (p->sock)
                val r = socket_wait_readable (p->sock, tout)
                prval () = TCPSocketConnection_addback_struct(pfat | tcp)
              in
                r
              end
    in
      if b then let
          extern fun lwip_recv: (int, ptr, size_t, int) -> int = "mac#"
          #define N 127
          #define CNUL '\000'
          val N1 = succ(N)
          val (pf, pfgc | p) = malloc_gc (i2sz N1)
          val n = lwip_recv (fd, p, i2sz N, 0)
          val () = $UN.ptr0_set<char>(ptr_add<char>(p, n), CNUL)
          val s = $UN.castvwtp0{strptr}((pf, pfgc | p))
        in
          Some_vt (s)
        end else None_vt ()
    end
end

implement
tcp_socket_connection_close (tcp) = {
  val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
  val () = endpoint_close (p->endpoint)
  val () = socket_close (p->sock)
  val () = $UN.castvwtp0((pfat | p))
  val () = __free (tcp) where {
    extern fun __free {vt:vtype} (x: vt): void = "atspre_mfree_gc"
  }
}

#include "share/atspre_staload.hats"

staload UN = "prelude/SATS/unsafe.sats"
staload "libmbedats/SATS/net_lwip_Socket_Socket.sats"
staload "libmbedats/SATS/net_lwip_Socket_Endpoint.sats"
staload "libmbedats/SATS/net_lwip_Socket_TCPSocketConnection.sats"

vtypedef TCPSocketConnection_struct = @{
  is_connected= bool
, sock=         Socket
, endpoint=     Endpoint
}

absvtype TCPSocketConnection_minus_struct (l:addr)

extern castfn
TCPSocketConnection_takeout_struct
(
  tcp: !TCPSocketConnection >> TCPSocketConnection_minus_struct l
) : #[l:addr] (TCPSocketConnection_struct @ l | ptr l)

extern praxi
TCPSocketConnection_addback_struct
  {l:addr}
(
  pfat: TCPSocketConnection_struct @ l
| tcp: !TCPSocketConnection_minus_struct l >> TCPSocketConnection
) : void

implement
tcp_socket_connection_open () = let
  val (pfgc, pfat | p) = ptr_alloc<TCPSocketConnection_struct> ()
  val () = p->is_connected := false
  val () = p->sock := socket_open ()
  val () = p->endpoint := endpoint_open ()
in
  $UN.castvwtp0{TCPSocketConnection}((pfat, pfgc | p))
end

implement
tcp_socket_connection_is_connected (tcp) = let
  val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
  val r = p->is_connected
  prval () = TCPSocketConnection_addback_struct(pfat | tcp)
in
  r
end

implement
tcp_socket_connection_connect (tcp, host, port) = let
  val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
  val rs = socket_initsock (p->sock, SOCK_STREAM)
  val ret = if rs = false then false else let
      val re = endpoint_set_address (p->endpoint, host, port)
    in
      if re = false then false else let
          abst@ype socklen_t = $extype"socklen_t"
          extern fun lwip_connect: (int, struct_sockaddr_in_p, socklen_t) -> int = "mac#"
          val fd = socket_sock_fd (p->sock)
          val rh = endpoint_remoteHost (p->endpoint)
          val rc = lwip_connect (fd, rh, $UN.cast (sizeof<struct_sockaddr_in>))
        in
          if rc < 0 then let
              val _ = socket_finisock (p->sock, true)
            in
              false
            end else let
              val () = p->is_connected := true
            in
              true
            end
        end
    end
  prval () = TCPSocketConnection_addback_struct(pfat | tcp)
in
  ret
end

implement
tcp_socket_connection_send_all (tcp, str) = let
  fun loop (tcp: !TCPSocketConnection, str: !strptr, fd: int, written: int): Option_vt int = let
      val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
      val blocking = socket_blocking (p->sock)
      prval () = TCPSocketConnection_addback_struct(pfat | tcp)
      val b = if blocking then true else let
                val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
                val tout = socket_timeout (p->sock)
                val r = socket_wait_writable (p->sock, tout)
                prval () = TCPSocketConnection_addback_struct(pfat | tcp)
              in
                r
              end
    in
      if b then let
          extern fun lwip_send: (int, ptr, size_t, int) -> int = "mac#"
          val p = strptr2ptr (str) // xxx
          val len = $UN.cast(strptr_length (str)) // xxx
          val r = lwip_send (fd, p, len, 0)
        in
          if r < 0 then None_vt () else if r = 0 then let
              val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
              val () = p->is_connected := false
              prval () = TCPSocketConnection_addback_struct(pfat | tcp)
            in
              Some_vt (written)
            end
          else let
              val written2 = written + r
            in
              Some_vt (written) // xxx tail call
            end
        end
      else Some_vt (written)
    end
  val is_connected = tcp_socket_connection_is_connected (tcp)
  val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
  val fd = socket_sock_fd (p->sock)
  val blocking = socket_blocking (p->sock)
  prval () = TCPSocketConnection_addback_struct(pfat | tcp)
in
  if (fd < 0) orelse (not is_connected) then None_vt () else loop (tcp, str, fd, 0)
end

implement
tcp_socket_connection_receive (tcp) = let
  val is_connected = tcp_socket_connection_is_connected (tcp)
  val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
  val fd = socket_sock_fd (p->sock)
  val blocking = socket_blocking (p->sock)
  prval () = TCPSocketConnection_addback_struct(pfat | tcp)
in
  if (fd < 0) orelse (not is_connected) then None_vt () else let
      val b = if blocking then true else let
                val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
                val tout = socket_timeout (p->sock)
                val r = socket_wait_readable (p->sock, tout)
                prval () = TCPSocketConnection_addback_struct(pfat | tcp)
              in
                r
              end
    in
      if b then let
          extern fun lwip_recv: (int, ptr, size_t, int) -> int = "mac#"
          #define N 127
          #define CNUL '\000'
          val N1 = succ(N)
          val (pf, pfgc | p) = malloc_gc (i2sz N1)
          val n = lwip_recv (fd, p, i2sz N, 0)
          val () = $UN.ptr0_set<char>(ptr_add<char>(p, n), CNUL)
          val s = $UN.castvwtp0{strptr}((pf, pfgc | p))
        in
          Some_vt (s)
        end else None_vt ()
    end
end

implement
tcp_socket_connection_close (tcp) = {
  val (pfat | p) = TCPSocketConnection_takeout_struct (tcp)
  val () = endpoint_close (p->endpoint)
  val () = socket_close (p->sock)
  val () = $UN.castvwtp0((pfat | p))
  val () = __free (tcp) where {
    extern fun __free {vt:vtype} (x: vt): void = "atspre_mfree_gc"
  }
}


Updating: very very reorganizing

Updating: very very reorganizing

(*
** A simple implementation of
** Game-of-24 in ATS for use in Java
*)

(* ****** ****** *)
//
// HX: no dynloading
//
#define ATS_DYNLOADFLAG 0
//
(* ****** ****** *)
//
#include
"share/atspre_define.hats"
//
(* ****** ****** *)

#define
SOURCE_targetloc "./../src"

(* ****** ****** *)
//
local
#include
"{$SOURCE}/GameOf24_card.dats"
in (* nothing *) end
local
#include
"{$SOURCE}/GameOf24_cardset.dats"
in (* nothing *) end
local
#include
"{$SOURCE}/GameOf24_solve.dats"
in (* nothing *) end
//
(* ****** ****** *)

staload
"{$SOURCE}/GameOf24.sats"

(* ****** ****** *)
//
staload
UN =
"prelude/SATS/unsafe.sats"
//
(* ****** ****** *)
//
staload
JNI = "{$JNI}/SATS/jni.sats"
//
stadef jint = $JNI.jint
stadef JNIEnvPtr = $JNI.JNIEnvPtr
stadef jstring (l:addr) = $JNI.jstring(l)
stadef jobject (l:addr) = $JNI.jobject(l)
//
(* ****** ****** *)
//
// HX-2013-08:
// [play24] is declared in Java class [GameOf24]
//
extern
fun
JNI_play24
  {l:addr}
(
  env: !JNIEnvPtr, obj: jobject(l)
, card1: jint, card2: jint, card3: jint, card4: jint
) : void = "ext#Java_GameOf24_play24" // endfun

(* ****** ****** *)

implement
JNI_play24
(
  env, obj, n1, n2, n3, n4
) = let
//
val n1 = $UN.cast{int}(n1)
val n2 = $UN.cast{int}(n2)
val n3 = $UN.cast{int}(n3)
val n4 = $UN.cast{int}(n4)
//
val out = stdout_ref
val res = play24 (n1, n2, n3, n4)
val ((*void*)) =
  fprintln! (out, "play24(", n1, ", ", n2, ", ", n3, ", ", n4, "):")
//
in
//
case+ res of
| list_nil() =>
  (
    fprintln! (out, "There is NO solution.")
  )
| list_cons _ =>
  (
    fpprint_cardlst (out, res); fprint_newline (out)
  )
//
end // end of [JNI_play24]

(* ****** ****** *)

%{$
//
// HX: This is ATS runtime:
//
#include "pats_ccomp_runtime.c"
#include "pats_ccomp_runtime2_dats.c"
#include "pats_ccomp_runtime_memalloc.c"
#include "pats_ccomp_runtime_trywith.c"
%}

(* ****** ****** *)

(* end of [GameOf24.dats] *)

(*
** A simple implementation of
** Game-of-24 in ATS for use in Java
*)

(* ****** ****** *)
//
// HX: no dynloading
//
#define ATS_DYNLOADFLAG 0
//
(* ****** ****** *)
//
#include
"share/atspre_define.hats"
//
(* ****** ****** *)
//
local
#include "./../GameOf24_card.dats"
in end
local
#include "./../GameOf24_cardset.dats"
in end
local
#include "./../GameOf24_solve.dats"
in end
//
(* ****** ****** *)
//
staload
UN =
"prelude/SATS/unsafe.sats"
//
(* ****** ****** *)
//
staload
JNI = "{$JNI}/SATS/jni.sats"
//
stadef jint = $JNI.jint
stadef JNIEnvPtr = $JNI.JNIEnvPtr
stadef jstring (l:addr) = $JNI.jstring(l)
stadef jobject (l:addr) = $JNI.jobject(l)
//
(* ****** ****** *)

staload "./../GameOf24.sats"

(* ****** ****** *)
//
// HX-2013-08:
// [play24] is declared in Java class [GameOf24]
//
extern
fun
JNI_play24
  {l:addr}
(
  env: !JNIEnvPtr, obj: jobject(l)
, card1: jint, card2: jint, card3: jint, card4: jint
) : void = "ext#Java_GameOf24_play24" // endfun

(* ****** ****** *)

implement
JNI_play24
(
  env, obj, n1, n2, n3, n4
) = let
//
val n1 = $UN.cast{int}(n1)
val n2 = $UN.cast{int}(n2)
val n3 = $UN.cast{int}(n3)
val n4 = $UN.cast{int}(n4)
//
val out = stdout_ref
val res = play24 (n1, n2, n3, n4)
val ((*void*)) =
  fprintln! (out, "play24(", n1, ", ", n2, ", ", n3, ", ", n4, "):")
//
in
//
case+ res of
| list_nil() =>
  (
    fprintln! (out, "There is NO solution.")
  )
| list_cons _ =>
  (
    fpprint_cardlst (out, res); fprint_newline (out)
  )
//
end // end of [JNI_play24]

(* ****** ****** *)

%{$
//
// HX: This is ATS runtime:
//
#include "pats_ccomp_runtime.c"
#include "pats_ccomp_runtime2_dats.c"
#include "pats_ccomp_runtime_memalloc.c"
#include "pats_ccomp_runtime_trywith.c"
%}

(* ****** ****** *)

(* end of [GameOf24.dats] *)


Use template for int_foreach_clo

Use template for int_foreach_clo

#include "config.hats"
#include "{$TOP}/avr_prelude/kernel_staload.hats"

staload "{$TOP}/SATS/arduino.sats"
staload UN = "prelude/SATS/unsafe.sats"

#define LED 9
#define DELAY_MS 10.0

typedef analog_w_t = natLt(256)

fun{} int_foreach_clo{n:nat}
(n: int(n), fwork: &natLt(n) -<clo1> void): void =
  loop(0, fwork) where {
  fun loop{i:nat | i <= n} .<n-i>.
    (i: int(i), fwork: &natLt(n) -<clo1> void):void =
    if i < n then (fwork(i); loop (i+1, fwork))
}

implement main () = {
  fun fadein() = let
    var fwork = lam@ (n: analog_w_t) =>
      (analogWrite (LED, n); delay_ms(DELAY_MS))
  in
    int_foreach_clo(256, fwork)
  end // end of [fadein]
  (* val () = init () *)
  val () = pinMode (LED, OUTPUT)
  val () = (fix f(): void => (fadein(); f()))()
}

#include "config.hats"
#include "{$TOP}/avr_prelude/kernel_staload.hats"

staload "{$TOP}/SATS/arduino.sats"
staload UN = "prelude/SATS/unsafe.sats"

#define LED 9
#define DELAY_MS 10.0

typedef analog_w_t = natLt(256)

fun int_foreach_clo
  (n: analog_w_t, fwork: &analog_w_t -<clo1> void): void = {
  fun loop (l: analog_w_t, r: analog_w_t, fwork: &analog_w_t -<clo1> void):void =
    if l < r then (fwork l; loop (succ l, r, fwork))
  val () = loop (0, n, fwork)
}

implement main () = {
  fun fadein() = let
    var fwork = lam@ (n: analog_w_t) =>
      (analogWrite (LED, n); delay_ms(DELAY_MS))
  in
    int_foreach_clo(255, fwork)
  end // end of [fadein]
  (* val () = init () *)
  val () = pinMode (LED, OUTPUT)
  val () = (fix f(): void => (fadein(); f()))()
}


Added note to python module

Added note to python module

%{#
#include "contrib/libats-/wdblair/python/CATS/Python.cats"
%}

(**
  Will Blair

  October 2014
*)

abstype PyObject = ptr

fun Py_SetProgramName (string): void = "mac#"

(* ****** ****** *)

macdef Py_file_input = $extval(int, "Py_file_input")

(* ****** ****** *)

fun Py_Initialize (): void = "mac#"

(**
  1 - load signal handlers
  0 - don't

  This is good for when you want to embed python and
  not worry about it initializing extra things like its
  signal handlers.
*)
fun Py_InitializeEx (int): void = "mac#"

fun Py_Finalize (): void = "mac#"

(* ****** ****** *)

fun Py_InitModule (string, ptr): PyObject = "mac#"

(* ****** ****** *)

fun PyImport_Import (PyObject): PyObject = "mac#"
fun PyImport_ImportModule (string): PyObject = "mac#"
fun PyImport_AddModule (string): PyObject = "mac#"

(* ****** ****** *)

fun PyModule_GetDict (PyObject): PyObject = "mac#"

(* ****** ****** *)

fun PyRun_SimpleString (string): PyObject = "mac#"

fun PyRun_File (
  FILEref, string, int, PyObject, PyObject
): PyObject = "mac#"

(* ****** ****** *)

fun PyDict_New (): PyObject = "mac#"

fun PyDict_GetItem (PyObject, PyObject): PyObject = "mac#"

fun PyDict_GetItemString (PyObject, string): PyObject = "mac#"
fun PyDict_SetItemString (PyObject, string, PyObject): PyObject = "mac#"

fun PyDict_Keys (PyObject): PyObject = "mac#"
fun PyDict_Items (PyObject): PyObject = "mac#"

fun PyDict_Copy (PyObject): PyObject = "mac#"

(* ****** ****** *)

fun PyList_Size (PyObject): ssize_t = "mac#"
fun PyList_GetItem (PyObject, ssize_t): PyObject = "mac#"

(* ****** ****** *)

fun PyTuple_New (ssize_t): PyObject = "mac#"
fun PyTuple_SetItem (PyObject, ssize_t, PyObject): int = "mac#"
fun PyTuple_GetItem (PyObject, ssize_t): PyObject = "mac#"

(* ****** ****** *)

fun PyObject_GetAttrString (PyObject, string): PyObject = "mac#"
fun PyObject_SetAttrString (PyObject, string, PyObject): int = "mac#"

fun PyObject_CallObject (PyObject, PyObject): PyObject = "mac#"

fun PyObject_Str (PyObject): PyObject = "mac#"

fun PyObject_IsInstance (PyObject, PyObject): int = "mac#"

(* ****** ****** *)

fun PyLong_FromVoidPtr (ptr): PyObject = "mac#"
fun PyLong_AsVoidPtr (PyObject): ptr = "mac#"

(* ****** ****** *)

fun PyString_FromString (string): PyObject = "mac#"
fun PyString_AsString (PyObject): string = "mac#"

(* ****** ****** *)

fun PyErr_Fetch (
  &PyObject? >> PyObject,
  &PyObject? >> PyObject,
  &PyObject? >> PyObject
): void = "mac#"

fun PyErr_NormalizeException (
  &PyObject >> _,
  &PyObject >> _,
  &PyObject >> _
): void = "mac#"

(* ****** ****** *)

macdef Py_None = $extval (PyObject, "Py_None")

(* ****** ****** *)

fun PyFunction_Check (PyObject): bool = "mac#"

(* ****** ****** *)

fun Py_INCREF (PyObject): void = "mac#"
fun Py_DECREF (PyObject): void = "mac#"

(* ****** ****** *)

%{#
#include "contrib/libats-/wdblair/python/CATS/Python.cats"
%}

abstype PyObject = ptr

fun Py_SetProgramName (string): void = "mac#"

(* ****** ****** *)

macdef Py_file_input = $extval(int, "Py_file_input")

(* ****** ****** *)

fun Py_Initialize (): void = "mac#"

(**
  1 - load signal handlers
  0 - don't

  This is good for when you want to embed python and
  not worry about it initializing extra things like its
  signal handlers.
*)
fun Py_InitializeEx (int): void = "mac#"

fun Py_Finalize (): void = "mac#"

(* ****** ****** *)

fun Py_InitModule (string, ptr): PyObject = "mac#"

(* ****** ****** *)

fun PyImport_Import (PyObject): PyObject = "mac#"
fun PyImport_ImportModule (string): PyObject = "mac#"
fun PyImport_AddModule (string): PyObject = "mac#"

(* ****** ****** *)

fun PyModule_GetDict (PyObject): PyObject = "mac#"

(* ****** ****** *)

fun PyRun_SimpleString (string): PyObject = "mac#"

fun PyRun_File (
  FILEref, string, int, PyObject, PyObject
): PyObject = "mac#"

(* ****** ****** *)

fun PyDict_New (): PyObject = "mac#"

fun PyDict_GetItem (PyObject, PyObject): PyObject = "mac#"

fun PyDict_GetItemString (PyObject, string): PyObject = "mac#"
fun PyDict_SetItemString (PyObject, string, PyObject): PyObject = "mac#"

fun PyDict_Keys (PyObject): PyObject = "mac#"
fun PyDict_Items (PyObject): PyObject = "mac#"

fun PyDict_Copy (PyObject): PyObject = "mac#"

(* ****** ****** *)

fun PyList_Size (PyObject): ssize_t = "mac#"
fun PyList_GetItem (PyObject, ssize_t): PyObject = "mac#"

(* ****** ****** *)

fun PyTuple_New (ssize_t): PyObject = "mac#"
fun PyTuple_SetItem (PyObject, ssize_t, PyObject): int = "mac#"
fun PyTuple_GetItem (PyObject, ssize_t): PyObject = "mac#"

(* ****** ****** *)

fun PyObject_GetAttrString (PyObject, string): PyObject = "mac#"
fun PyObject_SetAttrString (PyObject, string, PyObject): int = "mac#"

fun PyObject_CallObject (PyObject, PyObject): PyObject = "mac#"

fun PyObject_Str (PyObject): PyObject = "mac#"

fun PyObject_IsInstance (PyObject, PyObject): int = "mac#"

(* ****** ****** *)

fun PyLong_FromVoidPtr (ptr): PyObject = "mac#"
fun PyLong_AsVoidPtr (PyObject): ptr = "mac#"

(* ****** ****** *)

fun PyString_FromString (string): PyObject = "mac#"
fun PyString_AsString (PyObject): string = "mac#"

(* ****** ****** *)

fun PyErr_Fetch (
  &PyObject? >> PyObject,
  &PyObject? >> PyObject,
  &PyObject? >> PyObject
): void = "mac#"

fun PyErr_NormalizeException (
  &PyObject >> _,
  &PyObject >> _,
  &PyObject >> _
): void = "mac#"

(* ****** ****** *)

macdef Py_None = $extval (PyObject, "Py_None")

(* ****** ****** *)

fun PyFunction_Check (PyObject): bool = "mac#"

(* ****** ****** *)

fun Py_INCREF (PyObject): void = "mac#"
fun Py_DECREF (PyObject): void = "mac#"

(* ****** ****** *)