Advertisement
iocoder

generate.c

Jan 13th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. /* Microprogram generator
  2.  * -----------------------
  3.  * This program is used to generate the
  4.  * microprogram data that will be stored
  5.  * on the microprogram ROM of the CPU.
  6.  * You can view the microprogrammed machine
  7.  * as an abstract moore machine. Every state
  8.  * has a specific value for the output and
  9.  * a set of the ordered pairs: (input_value,
  10.  * next_state_val);
  11.  */
  12.  
  13. #include <stdio.h>
  14.  
  15. #define     A       0
  16. #define     B       1
  17. #define     C       2
  18.  
  19. typedef struct state {
  20.     int next_state[2];
  21.     int output;
  22. } state_t;
  23.  
  24. int main() {
  25.  
  26.     // A machine that outputs 1 when
  27.     // number of received 1s is divisible by 3.
  28.     // The abstract machine consists of 3 states:
  29.     // P.S |    N.S    |  OUTPUT  |    NOTE
  30.     //     | X=0   X=1 |          |
  31.     // ----|-----------|----------|--------------
  32.     //  A  |  A     B  |    1     | n mode 3 = 0
  33.     //  B  |  B     C  |    0     | n mode 3 = 1
  34.     //  C  |  C     A  |    0     | n mode 3 = 2
  35.  
  36.     // define the Moore machine:
  37.     state_t machine[3];
  38.  
  39.     // define the loop counters:
  40.     int i, j;
  41.  
  42.     // prepare the ROM image:
  43.     FILE *rom = fopen("image.bin", "w");
  44.  
  45.     // define state A:
  46.     machine[A].next_state[0] = A;
  47.     machine[A].next_state[1] = B;
  48.     machine[A].output = 1;
  49.  
  50.     // define state B:
  51.     machine[B].next_state[0] = B;
  52.     machine[B].next_state[1] = C;
  53.     machine[B].output = 0;
  54.  
  55.     // define state C:
  56.     machine[C].next_state[0] = C;
  57.     machine[C].next_state[1] = A;
  58.     machine[C].output = 0;
  59.  
  60.     // The layout of the ROM will be like this:
  61.     //          ADD |  N/A  | O/P | NS
  62.     //          --- | ----- | --- | --
  63.     // State A: 000 | 00000 |  1  | 00
  64.     //          001 | 00000 |  1  | 01
  65.     // State B: 010 | 00000 |  0  | 01
  66.     //          011 | 00000 |  0  | 10
  67.     // State C: 100 | 00000 |  0  | 10
  68.     //          101 | 00000 |  0  | 00
  69.     // NS is connected to the higher two bits of the
  70.     // 3-bit address bus through a clocked latch.
  71.     // The lower bit of the address is directly
  72.     // connected to the input.
  73.  
  74.     // now generate the microprogram:
  75.     for (i = 0; i < 3; i++) {
  76.         for (j = 0; j < 2; j++) {
  77.             char rombyte = machine[i].next_state[j] |
  78.                            (machine[i].output << 2);
  79.             fputc(rombyte, rom);
  80.         }
  81.     }
  82.  
  83.     // pad 2 extra characters to make it 8-bit:
  84.     fputc(0, rom);
  85.     fputc(0, rom);
  86.  
  87.     // close the file:
  88.     fclose(rom);
  89.  
  90.     // done:
  91.     return 0;
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement