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.

71 lines
1.6KB

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