ssm-lang / Scoria

This is an embedding of the Sparse Synchronous Model, in Haskell!
BSD 3-Clause "New" or "Revised" License
4 stars 0 forks source link

[RFC] Output handlers #91

Open Rewbert opened 2 years ago

Rewbert commented 2 years ago

What e.g output 1 does is to record the information that output pin 1 has been requested, and to create a reference that can be used to write to the output handler. The call returns this reference along with this output handler, that has to be scheduled in order to actualize the output. The output handler is hand-written by us right now and is just expected to exist.

We have a lot of functionality in the compiler to perhaps hack together a better interface for doing this, where most of the stuff is done in scoria. We do need some FFI, however.

-- This is the actual output handler that will actualize the output
outputHandler :: Ref Bool -> SSM ()
outputHandler out = routine $ do
  while true $ do
    wait out
    ffi "enable_gpio" $ deref out    -- need to be able to do some FFI here, and
                                     -- preferably in some way that is portable across platforms/backends
                                     -- this does right now not specify which GPIO to activate, see comment in program

{- This is the main SSM program, which is written in the same style as before.
   It will blink the LED with a 1 second period. -}
main :: (?out :: Ref Bool) => SSM ()
main = routine $ do
  while true $ do
    after (secs 1) ?out true
    wait ?out
    after (secs 1) ?out false
    wait ?out

program :: Compile backend ()
program = do
  {- where the current machinery is expected to create a global reference and associate
     it wil the GPIO, we already support creating global references here. We can try to
     create a global reference like this and associate it ourselves. -}
  out <- global @Bool
  let ?out = out

  {- now, what is left to do is to associate the reference with the pin in question.
     Perhaps this can be done with the reference? I am not sure what is the best, simplest,
     most portable way. -}
  schedule $ outputHandler out
  schedule main
j-hui commented 2 years ago

FFI in the EDSL seems like a good use case for quasiquote C library, at least for the C backend. We can use it to embed verbatim C, in the same way that inline assembly allows C programmers to embed platform-specific inline assembly.