iodevs.pl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #!/usr/bin/perl
  2. #
  3. # Generate I/O-device boilerplate for firmware and Verilog
  4. #
  5. use integer;
  6. use strict;
  7. use File::Spec;
  8. # Variables from configuration file
  9. our $iodev_addr_bits;
  10. our $iodev_addr_shift;
  11. our $xdev_addr_bits;
  12. our $xdev_addr_shift;
  13. our @sysirqs;
  14. our @iodevs;
  15. sub base($$$) {
  16. my($num,$bits,$shift) = @_;
  17. my $v = ($num | (~0 << $bits)) << $shift;
  18. return $v & 0xffffffff;
  19. }
  20. sub generate_h($) {
  21. my($out) = @_;
  22. print $out "#ifndef IODEVS_H\n";
  23. print $out "#define IODEVS_H\n\n";
  24. printf $out "#define IODEV_ADDR_BITS %d\n", $iodev_addr_bits;
  25. printf $out "#define IODEV_ADDR_SHIFT %d\n", $iodev_addr_shift;
  26. my $ndev = 0;
  27. my $nxdev = 0;
  28. my $nirq = 0;
  29. print $out "\n";
  30. foreach my $sysirq (@sysirqs) {
  31. printf $out "#define %s_IRQ\n", uc($sysirq), $nirq++;
  32. }
  33. foreach my $dev (@iodevs) {
  34. my $dcount = $dev->{-count};
  35. my $icount = length($dev->{-irq});
  36. $dcount = 1 unless (defined($dcount));
  37. next unless ($dcount);
  38. my $name = uc($dev->{-name});
  39. my $xdev = $dev->{-xdev};
  40. if ($xdev) {
  41. printf $out "\n#define %s_XDEV %d\n", $name, $nxdev;
  42. printf $out "#define %s_BASE 0x%08x\n", $name,
  43. base($nxdev, $xdev_addr_bits, $xdev_addr_shift);
  44. } else {
  45. printf $out "\n#define %s_DEV %d\n", $name, $ndev;
  46. printf $out "#define %s_BASE 0x%08x\n", $name,
  47. base($ndev, $iodev_addr_bits, $iodev_addr_shift);
  48. }
  49. printf $out "#define %s_DEV_COUNT %d\n", $name, $dcount;
  50. if ($icount) {
  51. printf $out "#define %s_IRQ %d\n", $name, $nirq;
  52. }
  53. $ndev += $xdev ? 0 : $dcount;
  54. $nxdev += $xdev ? $dcount : 0;
  55. $nirq += $dcount * $icount;
  56. }
  57. printf $out "\n#define IRQ_VECTORS %d\n", $nirq;
  58. print $out "\n#endif /* IODEVS_H */\n";
  59. }
  60. sub generate_irqtbl($)
  61. {
  62. my($out) = @_;
  63. my $nirq = 0;
  64. my @irqtbl = ();
  65. print $out "#include \"picorv32.h\"\n";
  66. print $out "#include \"irq.h\"\n\n";
  67. foreach my $sysirq (@sysirqs) {
  68. push(@irqtbl, [$sysirq, 1]);
  69. $nirq++;
  70. }
  71. foreach my $dev (@iodevs) {
  72. my $dcount = $dev->{-count};
  73. my $icount = length($dev->{-irq});
  74. $dcount = 1 unless (defined($dcount));
  75. next unless ($dcount && $icount);
  76. my $name = $dev->{-name};
  77. push(@irqtbl, [$name, $dcount*$icount]);
  78. $nirq += $dcount*$icount;
  79. }
  80. print $out "static void irqhandler_spurious(unsigned int vector)\n";
  81. print $out "{\n";
  82. print $out "\tmask_irq(vector);\n";
  83. print $out "}\n\n";
  84. foreach my $irq (@irqtbl) {
  85. printf $out "void irqhandler_%s(unsigned int) __attribute__((weak,alias(\"irqhandler_spurious\")));\n", $irq->[0];
  86. }
  87. print $out "\nirqhandler_t __attribute__((section(\".sdata\")))\n";
  88. printf $out "__irq_handler_table[%d] = {\n", $nirq;
  89. foreach my $irq (@irqtbl) {
  90. for (my $i = 0; $i < $irq->[1]; $i++) {
  91. printf $out "\tirqhandler_%s,\n", $irq->[0];
  92. }
  93. }
  94. print $out "};\n\n";
  95. }
  96. sub generate_verilog($)
  97. {
  98. my($out) = @_;
  99. my $ndev = 0;
  100. my $nxdev = 0;
  101. my $nirq = scalar(@sysirqs);
  102. my $irq_edge = 0;
  103. my @imux = ();
  104. my @xmux = ();
  105. my @wait = ();
  106. my @valid = ();
  107. my @irqs = ();
  108. print $out "\treg [31:0] nxdev_rdata;\n";
  109. print $out "\treg [31:0] iodev_rdata;\n";
  110. printf $out "\twire [%d:0] xdev_valid = iodev_mem_valid << cpu_mem_addr[%d:%d];\n",
  111. (1 << $xdev_addr_bits)-1,
  112. $xdev_addr_shift+$xdev_addr_bits-1, $xdev_addr_shift;
  113. printf $out "\twire [%d:0] iodev_valid = xdev_valid[%d] << cpu_mem_addr[%d:%d];\n",
  114. (1 << $iodev_addr_bits)-1, (1 << $xdev_addr_bits)-1,
  115. $iodev_addr_shift+$iodev_addr_bits-1, $iodev_addr_shift;
  116. print $out "\n";
  117. foreach my $dev (@iodevs) {
  118. my $dcount = $dev->{-count};
  119. my $irq = $dev->{-irq};
  120. my $xdev = $dev->{-xdev};
  121. $dcount = 1 unless (defined($dcount));
  122. next unless ($dcount);
  123. my $name = $dev->{-name};
  124. my $didx = ($dcount > 1) ? sprintf('[0:%d]', $dcount-1) : '';
  125. printf $out "\twire [31:0] iodev_rdata_%s%s;\n", $name, $didx;
  126. if ($irq ne '') {
  127. printf $out "\twire [%2d:0] iodev_irq_%s%s;\n",
  128. length($irq)-1, $name, $didx;
  129. }
  130. if ($xdev) {
  131. printf $out "\twire [%2d:0] iodev_valid_%s = xdev_valid[%d:%d];\n",
  132. $dcount-1, $name, $nxdev+$dcount-1, $nxdev;
  133. } else {
  134. printf $out "\twire [%2d:0] iodev_valid_%s = iodev_valid[%d:%d];\n",
  135. $dcount-1, $name, $ndev+$dcount-1, $ndev;
  136. }
  137. printf $out "\ttri1 [%2d:0] iodev_wait_n_%s;\n", $dcount-1, $name;
  138. push(@wait, "(&iodev_wait_n_$name)");
  139. print $out "\n";
  140. for (my $d = 0; $d < $dcount; $d++) {
  141. my $dsuf = ($dcount > 1) ? "[$d]" : '';
  142. if ($xdev) {
  143. push(@xmux, "iodev_rdata_$name$dsuf");
  144. } else {
  145. push(@imux, "iodev_rdata_$name$dsuf");
  146. }
  147. for (my $i = 0; $i < length($irq); $i++) {
  148. my $isuf = "[$i]";
  149. my $type = substr($irq,$i,1);
  150. push(@irqs, "iodev_irq_$name$dsuf$isuf");
  151. if ($type eq 'e') {
  152. $irq_edge |= 1 << $nirq;
  153. }
  154. $nirq++;
  155. }
  156. if ($xdev) {
  157. $nxdev++;
  158. } else {
  159. $ndev++;
  160. }
  161. }
  162. }
  163. print $out "\t// I/O input MUX\n";
  164. print $out "\talways_comb\n";
  165. printf $out "\t\tcase (cpu_mem_addr[%d:%d])\n",
  166. $xdev_addr_shift+$xdev_addr_bits-1, $xdev_addr_shift;
  167. my $nxdev = 0;
  168. foreach my $dev (@xmux) {
  169. printf $out "\t\t\t%d'd%d:\t iodev_rdata = %s;\n",
  170. $xdev_addr_bits, $nxdev++, $dev;
  171. }
  172. printf $out "\t\t\t%d'd%d:\n",
  173. $xdev_addr_bits, (1 << $xdev_addr_bits)-1;
  174. printf $out "\t\t\tcase (cpu_mem_addr[%d:%d])\n",
  175. $iodev_addr_shift+$iodev_addr_bits-1, $iodev_addr_shift;
  176. my $ndev = 0;
  177. foreach my $dev (@imux) {
  178. printf $out "\t\t\t\t%d'd%d:\t iodev_rdata = %s;\n",
  179. $iodev_addr_bits, $ndev++, $dev;
  180. }
  181. print $out "\t\t\t\tdefault: iodev_rdata = 32'hffffffff;\n";
  182. print $out "\t\t\tendcase\n";
  183. print $out "\t\t\tdefault: iodev_rdata = 32'hffffffff;\n";
  184. print $out "\t\tendcase\n";
  185. print $out "\n";
  186. print $out "\ttri0 [31:0] sys_irq;\n";
  187. my $nirq = scalar(@sysirqs);
  188. foreach my $irq (@irqs) {
  189. printf $out "\tassign sys_irq[%2d] = %s;\n", $nirq++, $irq;
  190. }
  191. print $out "\n";
  192. printf $out "\tlocalparam [31:0] irq_edge_mask = 32'h%08x;\n", $irq_edge;
  193. printf $out "\tlocalparam [31:0] irq_masked = ~32'h%08x;\n\n",
  194. ((1 << $nirq)-1);
  195. printf $out "\twire iodev_wait_n = ";
  196. if (scalar(@wait)) {
  197. print $out join(" & \n\t\t", @wait);
  198. } else {
  199. print $out "1'b1";
  200. }
  201. print $out ";\n";
  202. }
  203. my($mode, $infile, $outfile) = @ARGV;
  204. unless (defined(do File::Spec->rel2abs($infile))) {
  205. die "$0: $infile: $@\n"
  206. }
  207. open(my $out, '>', $outfile) or die;
  208. if ($mode eq 'h') {
  209. generate_h($out);
  210. } elsif ($mode eq 'c') {
  211. generate_irqtbl($out);
  212. } elsif ($mode eq 'v') {
  213. generate_verilog($out);
  214. }
  215. close($out);