F1U


F1UAP


Programming the Microcontrollers
--------------------------------


      Nowadays Cinder-ella is working in Crematorium..

Preface
-------

  I.  Single Task

    First digital computers (193x y) used this calculation model.

  II. Event Driven

    Event queue is a list of single-task programs in priority order.
    Priorities are set according to given Policy.
    Policy may be pre-determined by:

    * Hardware manufacturer (events are hardware interrupts)
    * Operating system and Hardware manufacturer (most of popular operating systems)
    * System Administrator ( i might be dreamin..*)

  III. Message Driven

    A set of computing objects talk with each other using messages (usualy strings). Number of computing units may be big enough

    * ~ 3000 for a single Graphic Card**
      ~ 10 ** 10 for the network-based supercomputing***

    Every such object have it's own R/W memory (cache), but also shares some common address space (among the other participating objects).

    Simultanous "read" from common area is nearly unlimited (broadcast allowed)

    "write" operation to common area is regulated by priority queue and protected by local policy (some objects may not to be allowed to write to common area)

-------

*)   Dreamer, Ozzy Osbourne, 2001

**)  NVIDIA GeForce GTX Titan Z: 2880 Shaders enabled
     AMD Radeon R9 295X2: 2816 shaders running at up to 1018MHz

***) Network is a Computer, John Gage (Sun), 1984

------------------------------------------------------------------------

    What is faster than the speed of light?
    The speed of thought*.

Single Tasking
--------------

Architecture:

  -- Harvard Architecture: Data and Code are in separate address spaces (for example we are moving across the corridor where "data" doors are across the left wall, and "code" doors are to the right).

  -- von Neumann architecture: Data and Code are in the same address space

(for example we are moving across the mountain road, where "data" and "code" are those holes in the mountain).

It's easy to show that we can build many thousands of compuing systems that differ in:

  -- architecture (Harvard or von Neumann)
  -- instruction set
  -- addressing modes [1]
  -- word width (how many bits can be processed at once 8, 16, 32, 64..)
  ..

So we need special assistant, called Translator, to talk with all such devices in the same manner. As soon as we need such Translator, we better teach a new tricks to this program than rebuild a hardware.

This is the way how some new abstract concepts (comfortable for the man usually just because it is easy to associate them with different sides of human life**)

-------

*)  It's unpleasant to admit, but it took me ~40 years to experience this simple truth. I was rather ready to accept parallel worlds or a travel through a wormhole.

**) Human behaviour, Bjork, 1993

EOBB (End Of BlahBlah..)

------------------------------------------------------------------------

    Jealousy: Cortana in replication mode :)

Learning curve
--------------

Task 0. Raptor:

  -- Read keyboard input,
  -- Store data to memory,
  -- Display it on console

  main start
    "?" get s
    s <- "string: " + s
    put s
  end

Task 1.  Expressions:

  -- Build priority table of allowed operations
  -- Discover parsing direction
  -- Discover parser laziness

Task 2. Vector and Matrix

  vector aka array:

    -- indexes from 1 to last element in use (U)
    -- initialize all elements individually
    -- not initialized elements
      + between 1 and U are 0
      + after U doesn't exist
        (U stands for "Upper" or rightmost index)

    -- access second element like

      s[2] = "Hello"
      put s[2]

    -- matrix is two dimensional array

    | 1  2 |  M[1][1] = 1   M[1][2] = 2
    | 3  4 |  M[2][1] = 3   M[1][1] = 4

    -- elements of different type are allowed within the same array

    s[1] = "I dont know why you say Goodbye "
    s[2] = 2
    s[3] = "I say Hello"
    s[4] = 4

    put s[1] + s[2]
    I dont know why you say Goodbye 2

    put s[2] + s[4]
    6

    put s[1] + s[3]
    I dont know why you say Goodbye I say Hello

Task 3. IF-THEN-ELSE

  main start
    get "yes or no?", s
    if s[1] == 'y' then
      put "YEAP"
    endif
    if s[1] == 'n' then
      put "NOPE"
    endif
    if s[1] != 'y' and s[1] != 'n') then
      put "YES or NO ???"
    endif
  end

Task 4. Recursion

  main start
    n <- 1
    sum <- 0
    call recu
  end

  recu start
    sum <- sum + 1 / n
    put n + ". " + sum EOL
    n <- n + 1
    if n < 100 then
      call recu
    endif
    put "divergent series"
  end

Task 5. Naming conventions for the subcharts

  input argument names:    iSubchartnameVarname
  output argument names:   oSubchartnameVarname
  in/out argument names:   ioSubchartnameVarname
  local variables:         vSubchartnameVarname
  local static variables:  sSubchartnameVarname
  pseudo global variables: gVarname

  Example from task 4.

  main start
    gN <- 1
    gSum <- 0

    iRecuLimit <- 5
    call Recu
    put oRecuRespond EOL
  end

  Recu start
    gSum <- gSum + 1 / mN
    put gN + ". " + gSum EOL
    gN <- gN + 1
    if gN < iRecuLimit then
      call Recu
    endif
    oRecuRespond = "Divergent Series"
  end

Task 6. LOOP

  main start
    loop
      get "yes or no?", s
      if s[1] == 'y' then
        put "YEAP"
      endif
      if s[1] == 'n' then
        put "NOPE"
      endif
      if s[1] != 'y' and s[1] != 'n') then
        put "YES or NO ???"
      endif
      lif 1 == 2 then
        break
      endlif
    endloop
  end

  Exercise: Write toupper chart converting string letters to upper case.
            use naming convention from Task 5.

Exercise 7. Convert Decimal number to a string of bits,
            like 18 = "10010"

Task 8. Graphics: Build a row of 8 Seven (Twelve) segment indicators.

                a              h   a
               ---            --- ---
             f|   |b        g|  i|   |b
             g --- g        l --- --- j
             e|   |c        f|   |k  |c
               ---            --- ---
                d              e   d

Task 9. Erroneous Incapsulation, or why it's time to say "Goodbye, Rap!"

  -- Raptor may be used to autogenerate parts of the source file written in Ada, Java, C++, C# (MANUAL POSTEDIT REQUIRED!).

  NB! Raptor is useless for multichart code generation (Incapsulation dissapears).

...

Task 99. Build a simple computer in Logisim 2.7.1

Address space has a four kinds of cells:

  -- memory (r/w aka read/write access)
  -- ports
    + to receive/collect data (ro aka read only)
    + to expose/output data (wo aka write only)
  -- calculators
    + 3 inputs for trinary operations (?:, carry in..)
    + 2 inputs for binary operations
    + 1 input for unary operation
    + 1 output for most of the operations
    + 2 outputs for some operations (13\5 = (2, 3), carry out)
    + 1 output flags (is_zero, below_zero..)
    + 1 output flags and errors (overflow, divide_by_zero..)
  -- instruction counter (r/w holds the address of the next instruction)

------------------------------------------------------------------------

Links
-----

  [1]. http://en.wikipedia.org/wiki/Addressing_mode


Funny Vocabulary
----------------

bluetooth  -- fang, sõrg, клык
lane       -- corridor, koridor, коридор
snail      -- tigu, улитка
jealousy   -- armukadedus, ревность
experience -- kogema, прочувствовать

------------------------------------------------------------------------

Appendix A. Generating code in Raptor

  Raptor
  ------

  main start
    x <- 3
    y[5] <- 5
    call port
  end

  port start
    v_port_z <- z
    put v_port_z
  end


  Raptor generated C++
  ----------------

  #include <iostream>
  #include <string>

  using namespace std;
  int main()
  {
     ?? z;
     ?? x;
     ?? v_port_z;
     ??[] y = new ??[??+1];

     x =3;
     y[5] = 5;
     z ="Hello, World!";
     v_port_z =z;
     cout << v_port_z << endl;
     return 0;
  }


  Manually Fixed
  --------------

  #include <iostream>
  #include <string>

  using namespace std;

  string z;

  void port( void) {
    string v_port_z = z;
    cout << v_port_z << endl;
  }

  int main()
  {
    int x;
    int* y = new int[5+1];

    x =3;
    y[5] = 5;
    z = "Hello, World!";
    // in z
    port();

    return 0;
  }

------------------------------------------------------------------------

Appendix B. Seven Segment Indicator in Raptor

                a              h   a
               ---            --- ---
             f|   |b        g|  i|   |b
             g --- g        l --- --- j
             e|   |c        f|   |k  |c
               ---            --- ---
                d              e   d

  Draw_Box( 60+x,160+y,100+x,180+y,GREEN,TRUE) # A
  Draw_Box(100+x,120+y,120+x,160+y,GREEN,TRUE) # B
  Draw_Box(100+x, 60+y,120+x,100+y,GREEN,TRUE) # C
  Draw_Box( 60+x, 40+y,100+x, 60+y,GREEN,TRUE) # D
  Draw_Box( 40+x, 60+y, 60+x,100+y,GREEN,TRUE) # E
  Draw_Box( 40+x,120+y, 60+x,160+y,GREEN,TRUE) # F
  Draw_Box( 60+x,100+y,100+x,120+y,GREEN,TRUE) # G

  #include <stdio.h>

  int main() {
    unsigned char font[] = {
      0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
      0x7f, 0x6f, 0x77, 0x7c, 0x58, 0x5e, 0x79, 0x71,
      0x3d, 0x74, 0x19, 0x0d, 0x76, 0x38, 0x36, 0x54,
      0x5c, 0x73, 0x67, 0x50, 0x6c, 0x78, 0x3e, 0x1c,
      0x7e, 0x14, 0x6e, 0x49, 0x00, 0x63, 0x08, 0x48
    };
    unsigned char ch, cl;
    int i;

    for(i=0;i<40;i++) {
      ch = (font[i] & 0xf0)>>4;
      ch += ch>9?0x37:0x30;
      cl = font[i] & 0x0f;
      cl += cl>9?0x37:0x30;

      printf("%02d:%02x:%c%c  ", i, font[i], ch, cl);
      if (0==(i+1)%5) printf("\n");
    }

    printf("\n");
    for(i= 0;i<16;i++) printf("%c", i>9?i+0x37:i+0x30);
  }


  /*
      00:3f:3F  01:06:06  02:5b:5B  03:4f:4F  04:66:66
      05:6d:6D  06:7d:7D  07:07:07  08:7f:7F  09:6f:6F
      10:77:77  11:7c:7C  12:58:58  13:5e:5E  14:79:79
      15:71:71  16:3d:3D  17:74:74  18:19:19  19:0d:0D
      20:76:76  21:38:38  22:36:36  23:54:54  24:5c:5C
      25:73:73  26:67:67  27:50:50  28:6c:6C  29:78:78
      30:3e:3E  31:1c:1C  32:7e:7E  33:14:14  34:6e:6E
      35:49:49  36:00:00  37:63:63  38:08:08  39:48:48

      0123456789ABCDEF
  */