summaryrefslogtreecommitdiff
path: root/compiler/z80/nz80inl.pas
blob: 8b78273aab829005b0a5123e5421db071ccccd3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
{
    Copyright (c) 2020 by Sven Barth

    Generates Z80 inline nodes

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 ****************************************************************************
}
unit nz80inl;

{$i fpcdefs.inc}

  interface

    uses
      node,ninl,ncginl, aasmbase;

    type
      tz80inlinenode = class(tcginlinenode)
        function pass_typecheck_cpu:tnode;override;
        function first_cpu : tnode;override;
        procedure pass_generate_code_cpu;override;
      end;

  implementation

    uses
      compinnr,
      aasmdata,
      aasmcpu,
      symdef,
      defutil,
      hlcgobj,
      pass_2,
      ncal,
      cgbase, cgobj, cgutils,
      cpubase;


    function tz80inlinenode.pass_typecheck_cpu : tnode;
      begin
        Result:=nil;
        case inlinenumber of
          in_z80_inport:
            begin
              CheckParameters(1);
              resultdef:=u8inttype;
            end;
          in_z80_outport:
            begin
              CheckParameters(2);
              resultdef:=voidtype;
            end;
          else
            Result:=inherited pass_typecheck_cpu;
        end;
      end;


    function tz80inlinenode.first_cpu : tnode;
      begin
        Result:=nil;
        case inlinenumber of
          in_z80_inport:
            expectloc:=LOC_REGISTER;
          in_z80_outport:
            expectloc:=LOC_VOID;
          else
            Result:=inherited first_cpu;
        end;
      end;


    procedure tz80inlinenode.pass_generate_code_cpu;

      procedure inport;
        var
          portnumber : tnode;
          dreg : tregister;
          ref : treference;
        begin
          portnumber:=left;
          secondpass(portnumber);
          if (portnumber.location.loc=LOC_CONSTANT) and
              (portnumber.location.value>=0) and
              (portnumber.location.value<=$ff) then
            begin
              { data needs to be put into A }
              dreg:=NR_A;
              reference_reset_base(ref,NR_NO,portnumber.location.value,ctempposinvalid,1,[]);
              current_asmdata.CurrAsmList.concat(taicpu.op_reg_ref(A_IN,dreg,ref));
            end
          else
            begin
              { data can be put anywhere, but port number must be in C }
              hlcg.getcpuregister(current_asmdata.CurrAsmList,NR_C);
              dreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_8);
              hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,portnumber.resultdef,u8inttype,portnumber.location,NR_C);
              reference_reset_base(ref,NR_C,0,ctempposinvalid,1,[]);
              current_asmdata.CurrAsmList.concat(taicpu.op_reg_ref(A_IN,dreg,ref));
              hlcg.ungetcpuregister(current_asmdata.CurrAsmList,NR_C);
            end;
            location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
            location.register:=dreg;
        end;

      procedure outport;
        var
          portnumber,
          portdata : tnode;
          dreg : tregister;
          ref : treference;
        begin
          portnumber:=tcallparanode(tcallparanode(left).right).left;
          portdata:=tcallparanode(left).left;
          secondpass(portdata);
          secondpass(portnumber);
          if (portnumber.location.loc=LOC_CONSTANT) and
              (portnumber.location.value>=0) and
              (portnumber.location.value<=$ff) then
            begin
              { data needs to reside in A }
              dreg:=NR_A;
              hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,portdata.resultdef,u8inttype,portdata.location,dreg);
              hlcg.getcpuregister(current_asmdata.CurrAsmList,dreg);
              reference_reset_base(ref,NR_NO,portnumber.location.value,ctempposinvalid,1,[]);
              current_asmdata.currasmlist.concat(taicpu.op_ref_reg(A_OUT,ref,dreg));
              hlcg.ungetcpuregister(current_asmdata.CurrAsmList,dreg);
            end
          else
            begin
              { data can reside anywhere, but port number must be in C }
              hlcg.getcpuregister(current_asmdata.CurrAsmList,NR_C);
              dreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_8);
              hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,portdata.resultdef,u8inttype,portdata.location,dreg);
              hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,portnumber.resultdef,u8inttype,portnumber.location,NR_C);
              reference_reset_base(ref,NR_C,0,ctempposinvalid,1,[]);
              current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_OUT,ref,dreg));
              hlcg.ungetcpuregister(current_asmdata.CurrAsmList,NR_C);
            end;
        end;

      begin
        case inlinenumber of
          in_z80_inport:
            inport;
          in_z80_outport:
            outport;
          else
            inherited pass_generate_code_cpu;
        end;
      end;


begin
  cinlinenode:=tz80inlinenode;
end.