OpenCores
URL https://opencores.org/ocsvn/s80186/s80186/trunk

Subversion Repositories s80186

[/] [s80186/] [trunk/] [sim/] [cppmodel/] [instructions/] [shr.cpp] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jamieiles
// Copyright Jamie Iles, 2017
2
//
3
// This file is part of s80x86.
4
//
5
// s80x86 is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// s80x86 is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with s80x86.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
template <typename T>
19
std::pair<uint16_t, T> do_shr(T v, int count)
20
{
21
    uint16_t flags = 0;
22
    T sign_bit = 0x80 << (8 * (sizeof(T) - 1));
23
    if (v & sign_bit)
24
        flags |= OF;
25
 
26
    for (int i = 0; i < count; ++i) {
27
        flags &= ~CF;
28
        if (v & 0x1)
29
            flags |= CF;
30
        v >>= 1;
31
    }
32
 
33
    if (!v)
34
        flags |= ZF;
35
    if (!__builtin_parity(v & 0xff))
36
        flags |= PF;
37
 
38
    return std::make_pair(flags, v);
39
}
40
 
41
// shr r/m, N
42
void EmulatorPimpl::shrc0()
43
{
44
    auto v = read_data<uint8_t>();
45
    auto count = fetch_byte();
46
    uint16_t flags;
47
 
48
    if (!(count & 0x1f))
49
        return;
50
 
51
    std::tie(flags, v) = do_shr(v, count & 0x1f);
52
 
53
    write_data<uint8_t>(v);
54
    registers->set_flags(flags, CF | ZF | PF | SF);
55
}
56
 
57
// shr r/m, 1
58
void EmulatorPimpl::shrc1()
59
{
60
    auto v = read_data<uint16_t>();
61
    auto count = fetch_byte();
62
    uint16_t flags;
63
 
64
    if (!(count & 0x1f))
65
        return;
66
 
67
    std::tie(flags, v) = do_shr(v, count & 0x1f);
68
 
69
    write_data<uint16_t>(v);
70
    registers->set_flags(flags, CF | ZF | PF | SF);
71
}
72
 
73
// shr r/m, 1
74
void EmulatorPimpl::shrd0()
75
{
76
    auto v = read_data<uint8_t>();
77
    uint16_t flags;
78
 
79
    std::tie(flags, v) = do_shr(v, 1);
80
 
81
    write_data<uint8_t>(v);
82
    registers->set_flags(flags, OF | CF | ZF | PF | SF);
83
}
84
 
85
// shr r/m, 1
86
void EmulatorPimpl::shrd1()
87
{
88
    auto v = read_data<uint16_t>();
89
    uint16_t flags;
90
 
91
    std::tie(flags, v) = do_shr(v, 1);
92
 
93
    write_data<uint16_t>(v);
94
    registers->set_flags(flags, OF | CF | ZF | PF | SF);
95
}
96
 
97
// shr r/m, N
98
void EmulatorPimpl::shrd2()
99
{
100
    auto v = read_data<uint8_t>();
101
    uint16_t flags;
102
 
103
    if (!registers->get(CL))
104
        return;
105
 
106
    std::tie(flags, v) = do_shr(v, registers->get(CL));
107
 
108
    write_data<uint8_t>(v);
109
    registers->set_flags(flags, CF | ZF | PF | SF);
110
}
111
 
112
// shr r/m, N
113
void EmulatorPimpl::shrd3()
114
{
115
    auto v = read_data<uint16_t>();
116
    uint16_t flags;
117
 
118
    if (!registers->get(CL))
119
        return;
120
 
121
    std::tie(flags, v) = do_shr(v, registers->get(CL));
122
 
123
    write_data<uint16_t>(v);
124
    registers->set_flags(flags, CF | ZF | PF | SF);
125
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.