Category Archives: pyCPU Programcode example

Simple pyCPU Programcode example

The Following is a simple program for the pyCPU (Python Hardware Processor). Keep in mind that the Databitwitdth is limited to a constant factor given to the Myhdl description.

PREDEFINED_IO_ADDRESSES={'PORTA_IN':0,'PORTB_IN':1,'PORTC_OUT':2,'PORTD_OUT':3 } 

def busywait(time):
  x=0
  while x<100:
    td=0
    x=x+1
    while td<100:
      td=td+1
      ss=0
      while ss<time:
	ss=ss+1

def CPU_main():
  global PORTA_IN,PORTB_IN,PORTC_OUT,PORTD_OUT
  x=0
  while 1:
    PORTD_OUT=PORTD_OUT^16
    busywait(20)
    if (PORTB_IN & 0x01)==1:
      PORTD_OUT=PORTD_OUT|0x20
    else:
      PORTD_OUT=PORTD_OUT&0xdf

To get a little insight you can use the dis python module to show the bytecode instructions. The instructions for the function CPU_main are shown below .

import dis
dis.dis(CPU_main)
  3           0 LOAD_CONST               1 (0)
              3 STORE_FAST               0 (x)

  4           6 SETUP_LOOP              62 (to 71)

  5     >>    9 LOAD_GLOBAL              0 (PORTD_OUT)
             12 LOAD_CONST               2 (16)
             15 BINARY_XOR          
             16 STORE_GLOBAL             0 (PORTD_OUT)

  6          19 LOAD_GLOBAL              1 (busywait)
             22 LOAD_CONST               3 (20)
             25 CALL_FUNCTION            1
             28 POP_TOP             

  7          29 LOAD_GLOBAL              2 (PORTB_IN)
             32 LOAD_CONST               4 (1)
             35 BINARY_AND          
             36 LOAD_CONST               4 (1)
             39 COMPARE_OP               2 (==)
             42 POP_JUMP_IF_FALSE       58

  8          45 LOAD_GLOBAL              0 (PORTD_OUT)
             48 LOAD_CONST               5 (32)
             51 BINARY_OR           
             52 STORE_GLOBAL             0 (PORTD_OUT)
             55 JUMP_ABSOLUTE            9

 10     >>   58 LOAD_GLOBAL              0 (PORTD_OUT)
             61 LOAD_CONST               6 (223)
             64 BINARY_AND          
             65 STORE_GLOBAL             0 (PORTD_OUT)
             68 JUMP_ABSOLUTE            9
        >>   71 LOAD_CONST               0 (None)
             74 RETURN_VALUE