Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 line
1.6KB

  1. #include "sms_ports.h"
  2. void ports_init(Ports *ports)
  3. {
  4. ports->ctl = 0xff;
  5. ports->TRA = TRI_HIGHZ;
  6. ports->THA = TRI_HIGHZ;
  7. ports->TRB = TRI_HIGHZ;
  8. ports->THB = TRI_HIGHZ;
  9. }
  10. void ports_ctl_wr(Ports *ports, uint8_t val)
  11. {
  12. ports->ctl = val;
  13. ports->TRA = TRI_HIGHZ;
  14. ports->THA = TRI_HIGHZ;
  15. ports->TRB = TRI_HIGHZ;
  16. ports->THB = TRI_HIGHZ;
  17. if ((val & 0x01) == 0) {
  18. ports->TRA = ((val & 0x10) == 0) ? TRI_LOW : TRI_HIGH;
  19. }
  20. if ((val & 0x02) == 0) {
  21. ports->THA = ((val & 0x20) == 0) ? TRI_LOW : TRI_HIGH;
  22. }
  23. if ((val & 0x04) == 0) {
  24. ports->TRB = ((val & 0x40) == 0) ? TRI_LOW : TRI_HIGH;
  25. }
  26. if ((val & 0x08) == 0) {
  27. ports->THB = ((val & 0x80) == 0) ? TRI_LOW : TRI_HIGH;
  28. }
  29. }
  30. uint8_t ports_A_rd(Ports *ports)
  31. {
  32. // Bits 7:6 are port B's Down/Up
  33. // Bits 5:0 are port A's TR/TL/R/L/D/U
  34. uint8_t res = 0xff;
  35. if (ports->portA_rd != NULL) {
  36. res &= ports->portA_rd() | 0b11000000;
  37. }
  38. if (ports->portB_rd != NULL) {
  39. res &= (ports->portB_rd() << 6) | 0b00111111;
  40. }
  41. return res;
  42. }
  43. uint8_t ports_B_rd(Ports *ports)
  44. {
  45. // Bit 7: Port B's TH
  46. // Bit 6: Port A's TH
  47. // Bit 5: unused
  48. // Bit 4: unused (reset button)
  49. // Bits 3:0 are port B's TR/TL/R/L
  50. uint8_t res = 0xff;
  51. if (ports->portA_rd != NULL) {
  52. res &= ports->portA_rd() | 0b10111111;
  53. }
  54. if (ports->portB_rd != NULL) {
  55. uint8_t portb = ports->portB_rd();
  56. res &= (portb << 1) | 0b01111111; // TH
  57. res &= (portb >> 2) | 0b11110000; // TR/TL/R/L
  58. }
  59. return res;
  60. }