SECTION "COTEST"


GET "LIBHDR"


LET start()  BE
$(
    LET name  =  VEC 20

    writes( "Which service shall I zap? *E" )

    readstring( name )

    FOR  i = 1  TO  5  DO  cocreate( wombat, 200, name )

    nextco()  REPEAT
$)



AND wombat( base, name )  BE
$(
    LET msg     =  "Hello from the Z80 BCPL system"
    LET msgl    =  msg % 0

    LET txbuff  =  VEC 64
    LET rxbuff  =  VEC 64

    FOR  i = 0  TO  msgl  DO  txbuff % (i + 8)  :=  msg % i

    txbuff!0  :=  3 + msgl/2 + 1
    rxbuff!0  :=  64

    writef( "Coroutine %N started*N", base )

    FOR  i = 1  TO  5  DO
    $(
        LET rc  =  ssp( name, txbuff, rxbuff )  
        LET r2  =  result2
        
        writef( "Coroutine %N (%N)  RC=%N  R2=%N  %X4 %X4 %X4*N", 
                 base, i, rc, r2, rxbuff!1, rxbuff!2, rxbuff!3 )

        FOR  i = 1  TO  5  DO  nextco()
    $)

    writef( "Coroutine %N stopping*N", base )
$)



AND readstring( buffer )  BE
$(
    LET length  =  0
    LET ch      =  rdch()

    UNTIL  ch = '*N'  DO
    $(
        length           :=  length + 1
        buffer % length  :=  ch
        ch               :=  rdch()
    $)

    buffer % 0  :=  length
$)



