fsm_to_hex.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package javaapplication2;
  7. import java.io.BufferedWriter;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. public class JavaApplication2 {
  11. public static void main(String[] args) throws IOException
  12. {
  13. int i;
  14. int j;
  15. int k;
  16. int l;
  17. int m;
  18. int n;
  19. int o;
  20. int p;
  21. int q;
  22. int r;
  23. int s;
  24. int t;
  25. int u;
  26. BufferedWriter writer = new BufferedWriter(new FileWriter("skibolaLS.txt"));
  27. writer.append("v2.0 raw");
  28. writer.newLine();
  29. for (i = 0; i < 2; i++)
  30. {
  31. for (j = 0; j < 2; j++)
  32. {
  33. for (k = 0; k < 2; k++)
  34. {
  35. for (l = 0; l < 2; l++)
  36. {
  37. for (m = 0; m < 2; m++)
  38. {
  39. for (n = 0; n < 2; n++)
  40. {
  41. for (o = 0; o < 2; o++)
  42. {
  43. for (p = 0; p < 2; p++)
  44. {
  45. for (q = 0; q < 2; q++)
  46. {
  47. for (r = 0; r < 2; r++)
  48. {
  49. for (s = 0; s < 2; s++)
  50. {
  51. for (t = 0; t < 2; t++)
  52. {
  53. for (u = 0; u < 2; u++)
  54. {
  55. maquina(writer, i, j, k, l, m, n, o, p, q, r, s, t, u);
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. //writer.append(":00000001FF");
  70. writer.close();
  71. }
  72. public static void maquina(BufferedWriter writer, int stateA, int stateB, int stateC, int timeout, int randomA, int randomB, int bolaem1, int bolaem2, int bolaem3, int bolasai, int reset, int ex1, int ex2) throws IOException
  73. {
  74. //write the binary value of the address in a string
  75. String str = ""+stateA+stateB+stateC+timeout+randomA+randomB+bolaem1+bolaem2+bolaem3+bolasai+reset+ex1+ex2;
  76. //format the binary string to a 4 digit HEX
  77. String formathex = str.format("%04x", Integer.parseInt(str, 2));
  78. //set and calculate the address part of the checksum
  79. int checksum = 0x1+Integer.parseInt(formathex.substring(0, 2), 16)+Integer.parseInt(formathex.substring(2, 4), 16);
  80. //write address part of the line
  81. //writer.append(":01"+formathex.toUpperCase()+"00");
  82. int led1 = 0;
  83. int led2 = 0;
  84. int led3 = 0;
  85. int scoreA = 0;
  86. int scoreB = 0;
  87. /*
  88. 00=nada
  89. 01=soma ao score
  90. 10=reseta o score
  91. 11=reseta timer e score
  92. */
  93. if (reset == 1)
  94. {
  95. scoreA = 1;
  96. scoreB = 1;
  97. led1 = 1;
  98. led2 = 1;
  99. led3 = 1;
  100. stateA = 1;
  101. stateB = 1;
  102. stateC = 1;
  103. }
  104. else if (timeout == 1)
  105. {
  106. led1 = 1;
  107. led2 = 1;
  108. led3 = 1;
  109. stateA = 0;
  110. stateB = 0;
  111. stateC = 0;
  112. }
  113. else if (stateA == 0)
  114. {
  115. if (stateB == 0)
  116. {
  117. if (stateC == 0)
  118. {
  119. //gameover
  120. }
  121. else
  122. {
  123. //led1on
  124. led1 = 1;
  125. if (bolaem1 == 1)
  126. {
  127. scoreA = 1;
  128. led1 = 1;
  129. led2 = 1;
  130. led3 = 1;
  131. stateA = 1;
  132. stateC = 0;
  133. }
  134. else if ( (bolaem2 == 1) || (bolaem3 == 1) )
  135. {
  136. scoreB = 1;
  137. stateA = 1;
  138. }
  139. }
  140. }
  141. else
  142. {
  143. if (stateC == 0)
  144. {
  145. //led2on
  146. led2 = 1;
  147. if (bolaem2 == 1)
  148. {
  149. scoreA = 1;
  150. led1 = 1;
  151. led2 = 1;
  152. led3 = 1;
  153. stateA = 1;
  154. stateB = 0;
  155. }
  156. else if ( (bolaem1 == 1) || (bolaem3 == 1) )
  157. {
  158. scoreB = 1;
  159. stateA = 1;
  160. stateB = 0;
  161. stateC = 1;
  162. }
  163. }
  164. else
  165. {
  166. //led3on
  167. led3 = 1;
  168. if (bolaem3 == 1)
  169. {
  170. scoreA = 1;
  171. led1 = 1;
  172. led2 = 1;
  173. led3 = 1;
  174. stateA = 1;
  175. stateB = 0;
  176. stateC = 0;
  177. }
  178. else if ( (bolaem1 == 1) || (bolaem2 == 1) )
  179. {
  180. scoreB = 1;
  181. stateA = 1;
  182. stateB = 0;
  183. }
  184. }
  185. }
  186. }
  187. else
  188. {
  189. if (stateB == 0)
  190. {
  191. if (stateC == 0)
  192. {
  193. //acertou
  194. led1 = 1;
  195. led2 = 1;
  196. led3 = 1;
  197. stateB = 1;
  198. }
  199. else
  200. {
  201. //errou
  202. stateB = 1;
  203. stateC = 0;
  204. }
  205. }
  206. else
  207. {
  208. if (stateC == 0)
  209. {
  210. //wait
  211. if (bolasai == 1)
  212. {
  213. stateC = 1;
  214. }
  215. }
  216. else
  217. {
  218. //select
  219. if (randomA == 0)
  220. {
  221. stateA = 0;
  222. stateB = 0;
  223. }
  224. else
  225. {
  226. if (randomB == 0)
  227. {
  228. stateA = 0;
  229. stateC = 0;
  230. }
  231. else
  232. {
  233. stateA = 0;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. //write the binary value of the output in a string
  240. str = ""+stateA+stateB+stateC+led1+led2+led3+scoreA+scoreB;
  241. //format the binary string to a 2 digit HEX
  242. formathex = String.format("%02x", Integer.parseInt(str, 2));
  243. //set and calculate the output part of the checksum
  244. checksum += Integer.parseInt(formathex, 16);
  245. String formatchecksum = String.format("%02x", checksum);
  246. String formatchecksum2 = "-"+formatchecksum.substring(formatchecksum.length()-2, formatchecksum.length());
  247. String formatchecksum3 = String.format("%02x", Integer.parseInt(formatchecksum2, 16));
  248. //write output part of the line
  249. /*
  250. writer.append(formathex.toUpperCase()+formatchecksum3.substring(formatchecksum3.length()-2, formatchecksum3.length()).toUpperCase());
  251. writer.newLine();
  252. */
  253. writer.append(formathex);
  254. writer.newLine();
  255. }
  256. }