Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.02 KB | None | 0 0
  1. function famakin_regarray
  2.  
  3. % Plan of attack: Create an array of arrays where each array is a register
  4. % A register is described by
  5. % R = [DATA_IN | ENABLE | RESET | DATA_OUT]
  6. %
  7. % R(1) = OP_A, R(2) = EN, R(3) = RST, R(4) = OP_Q
  8. % The array of registers is thus:
  9. %
  10. % [R0, R1 ... RN]
  11. %
  12. % Input file is of the form:
  13. % ----------------------------------------
  14. % OP_A | LD_REG | RESET | DR | SR1 | SR2
  15. %
  16. % - Make LD_REG fluctuate between 0 and 1
  17. % - Make RESET 1 for a couple clock cycles
  18. % - Declare a variable sEN which contains the ENABLE signals for
  19. %   each register in the array
  20.  
  21. fin = fopen('FAMAKIN_REGARRAY_INPUT.txt', 'w');
  22. fout = fopen('FAMAKIN_REGARRAY_OUTPUT.txt', 'w');
  23.  
  24. fprintf(fin, '%s\r\n', 'XXXXXXXXXXXXXXXX X X XXX XXX XXX');
  25.  
  26. %initialize regarray
  27. regarray = cell(8, 1);
  28.  
  29. %create an array to hold last known values of the register
  30. % just in case we want to latch
  31. last_out = cell(8, 1);
  32.  
  33. %create your ENABLE vector signal
  34. sEN = repmat('0', 1, 8);
  35.  
  36. for i = 1:8
  37.     reg = {dec2bin(0, 16), '0', '1', dec2bin(i, 16)};
  38.     regarray{i} = reg;
  39. end
  40.  
  41. %test reset
  42. for A = 0:3
  43.     %always reset ENABLE vector to zeros
  44.     %we only need one bit of the vector to be high at a time
  45.     sEN = repmat('0', 1, 8);
  46.     RESET = '1';
  47.     dr = mod(A, 8);
  48.     dr_bin = dec2bin(7-dr, 3);
  49.     LD_REG = dec2bin(mod(A, 2), 1);
  50.     sEN(dr + 1) = LD_REG;
  51.     for i = 1:8
  52.         regarray{i}{1} = dec2bin(A, 16);
  53.         regarray{i}{2} = sEN(i);
  54.         regarray{i}{3} = RESET;
  55.         regarray{i}{4} = dec2bin(0, 16);
  56.         last_out{i} = 0;
  57.     end
  58.    
  59.     SR1 = mod(A+1, 8);
  60.     SR2 = 7-mod(A, 8);
  61.    
  62.     SR1_bin = dec2bin(7-SR1, 3);
  63.     SR2_bin = dec2bin(7-SR2, 3);
  64.    
  65.     file_input = [dec2bin(A, 16) ' ' LD_REG ' ' RESET ' ' dr_bin ' ' SR1_bin ' ' SR2_bin];
  66.     fprintf(fin, '%s\r\n', file_input);
  67.     fprintf(fout, '%s\r\n', [dec2bin(0, 16) ' ' dec2bin(0, 16)]);
  68. end
  69.  
  70. for A = 4:7
  71.     sEN = repmat('0', 1, 8);
  72.     RESET = '0';
  73.    
  74.     %make dr go from 0 to 7
  75.     dr = mod(A, 8);
  76.     %remember MATLAB goes from 1-8 MSB-LSB
  77.     %but VHDL goes from 7-0 MSB to LSB. So we
  78.     %have to switch indexing systems
  79.     dr_bin = dec2bin(7-dr, 3);
  80.     %we're fluctuating the enable here
  81.     LD_REG = dec2bin(mod(A+1, 2), 1);
  82.     %assigning the only high enable to the destination register
  83.     sEN(dr+1) = LD_REG;
  84.    
  85.     for i = 1:8
  86.         regarray{i}{1} = dec2bin(A, 16);
  87.         regarray{i}{2} = sEN(i);
  88.         regarray{i}{3} = RESET;
  89.        
  90.         if regarray{i}{2} == '0'
  91.             regarray{i}{4} = dec2bin(last_out{i}, 16);
  92.         else
  93.             regarray{i}{4} = dec2bin(A, 16);
  94.             last_out{i} = bin2dec(regarray{i}{4});
  95.         end
  96.     end
  97.     %this is to make sure that the source register
  98.     %is not the same as the destination register
  99.     SR1 = mod(A+1, 8);
  100.     SR2 = 7-mod(A, 8);
  101.    
  102.     %also we're switching to VHDL's index system (7 downto 0)
  103.     SR1_bin = dec2bin(7-SR1, 3);
  104.     SR2_bin = dec2bin(7-SR2, 3);
  105.    
  106.     fprintf(fin, '%s\r\n', [dec2bin(A, 16) ' ' LD_REG ' ' RESET ' ' dr_bin ' ' SR1_bin ' ' SR2_bin]);
  107.     fprintf(fout, '%s\r\n', [regarray{SR1+1}{4} ' ' regarray{SR2+1}{4}]);
  108. end
  109.  
  110. for A = 8:100%(2^16)-1
  111.    sEN = repmat('0', 1, 8);
  112.    RESET = '0';
  113.    dr = mod(A, 8);
  114.    dr_bin = dec2bin(7-dr, 3);
  115.    LD_REG = '1';
  116.    sEN(dr+1) = LD_REG;
  117.    
  118.     for i = 1:8
  119.         regarray{i}{1} = dec2bin(A, 16);
  120.         regarray{i}{2} = sEN(i);
  121.         regarray{i}{3} = RESET;
  122.        
  123.         if regarray{i}{2} == '0'
  124.             regarray{i}{4} = dec2bin(last_out{i}, 16);
  125.         else
  126.             regarray{i}{4} = dec2bin(A, 16);
  127.             last_out{i} = bin2dec(regarray{i}{4});
  128.         end
  129.     end
  130.     SR1 = mod(A+1, 8);
  131.     SR2 = 7-mod(A, 8);
  132.    
  133.     SR1_bin = dec2bin(7-SR1, 3);
  134.     SR2_bin = dec2bin(7-SR2, 3);
  135.    
  136.     fprintf(fin, '%s\r\n', [dec2bin(A, 16) ' ' LD_REG ' ' RESET ' ' dr_bin ' ' SR1_bin ' ' SR2_bin]);
  137.     fprintf(fout, '%s\r\n', [regarray{SR1+1}{4} ' ' regarray{SR2+1}{4}]);
  138. end
  139.  
  140. fclose(fin);
  141. fclose(fout);
  142.  
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement