nz80inl.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. {
  2. Copyright (c) 2020 by Sven Barth
  3. Generates Z80 inline nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nz80inl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ninl,ncginl, aasmbase;
  22. type
  23. tz80inlinenode = class(tcginlinenode)
  24. function pass_typecheck_cpu:tnode;override;
  25. function first_cpu : tnode;override;
  26. procedure pass_generate_code_cpu;override;
  27. end;
  28. implementation
  29. uses
  30. compinnr,
  31. aasmdata,
  32. aasmcpu,
  33. symdef,
  34. defutil,
  35. hlcgobj,
  36. pass_2,
  37. ncal,
  38. cgbase, cgobj, cgutils,
  39. cpubase;
  40. function tz80inlinenode.pass_typecheck_cpu : tnode;
  41. begin
  42. Result:=nil;
  43. case inlinenumber of
  44. in_z80_inport:
  45. begin
  46. CheckParameters(1);
  47. resultdef:=u8inttype;
  48. end;
  49. in_z80_outport:
  50. begin
  51. CheckParameters(2);
  52. resultdef:=voidtype;
  53. end;
  54. else
  55. Result:=inherited pass_typecheck_cpu;
  56. end;
  57. end;
  58. function tz80inlinenode.first_cpu : tnode;
  59. begin
  60. Result:=nil;
  61. case inlinenumber of
  62. in_z80_inport:
  63. expectloc:=LOC_REGISTER;
  64. in_z80_outport:
  65. expectloc:=LOC_VOID;
  66. else
  67. Result:=inherited first_cpu;
  68. end;
  69. end;
  70. procedure tz80inlinenode.pass_generate_code_cpu;
  71. procedure inport;
  72. var
  73. portnumber : tnode;
  74. dreg : tregister;
  75. ref : treference;
  76. begin
  77. portnumber:=left;
  78. secondpass(portnumber);
  79. if (portnumber.location.loc=LOC_CONSTANT) and
  80. (portnumber.location.value>=0) and
  81. (portnumber.location.value<=$ff) then
  82. begin
  83. { data needs to be put into A }
  84. dreg:=NR_A;
  85. reference_reset_base(ref,NR_NO,portnumber.location.value,ctempposinvalid,1,[]);
  86. current_asmdata.CurrAsmList.concat(taicpu.op_reg_ref(A_IN,dreg,ref));
  87. end
  88. else
  89. begin
  90. { data can be put anywhere, but port number must be in C }
  91. hlcg.getcpuregister(current_asmdata.CurrAsmList,NR_C);
  92. dreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_8);
  93. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,portnumber.resultdef,u8inttype,portnumber.location,NR_C);
  94. reference_reset_base(ref,NR_C,0,ctempposinvalid,1,[]);
  95. current_asmdata.CurrAsmList.concat(taicpu.op_reg_ref(A_IN,dreg,ref));
  96. hlcg.ungetcpuregister(current_asmdata.CurrAsmList,NR_C);
  97. end;
  98. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  99. location.register:=dreg;
  100. end;
  101. procedure outport;
  102. var
  103. portnumber,
  104. portdata : tnode;
  105. dreg : tregister;
  106. ref : treference;
  107. begin
  108. portnumber:=tcallparanode(tcallparanode(left).right).left;
  109. portdata:=tcallparanode(left).left;
  110. secondpass(portdata);
  111. secondpass(portnumber);
  112. if (portnumber.location.loc=LOC_CONSTANT) and
  113. (portnumber.location.value>=0) and
  114. (portnumber.location.value<=$ff) then
  115. begin
  116. { data needs to reside in A }
  117. dreg:=NR_A;
  118. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,portdata.resultdef,u8inttype,portdata.location,dreg);
  119. hlcg.getcpuregister(current_asmdata.CurrAsmList,dreg);
  120. reference_reset_base(ref,NR_NO,portnumber.location.value,ctempposinvalid,1,[]);
  121. current_asmdata.currasmlist.concat(taicpu.op_ref_reg(A_OUT,ref,dreg));
  122. hlcg.ungetcpuregister(current_asmdata.CurrAsmList,dreg);
  123. end
  124. else
  125. begin
  126. { data can reside anywhere, but port number must be in C }
  127. hlcg.getcpuregister(current_asmdata.CurrAsmList,NR_C);
  128. dreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_8);
  129. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,portdata.resultdef,u8inttype,portdata.location,dreg);
  130. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,portnumber.resultdef,u8inttype,portnumber.location,NR_C);
  131. reference_reset_base(ref,NR_C,0,ctempposinvalid,1,[]);
  132. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_OUT,ref,dreg));
  133. hlcg.ungetcpuregister(current_asmdata.CurrAsmList,NR_C);
  134. end;
  135. end;
  136. begin
  137. case inlinenumber of
  138. in_z80_inport:
  139. inport;
  140. in_z80_outport:
  141. outport;
  142. else
  143. inherited pass_generate_code_cpu;
  144. end;
  145. end;
  146. begin
  147. cinlinenode:=tz80inlinenode;
  148. end.