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

Subversion Repositories s80186

[/] [s80186/] [trunk/] [sim/] [cppmodel/] [instructions/] [shl.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_shl(T v, int count)
20
{
21
    uint16_t flags = 0;
22
    T overflow_test_bits = (v >> ((8 * sizeof(T)) - 2)) & 3;
23
 
24
    if (overflow_test_bits == 1 || overflow_test_bits == 2)
25
        flags |= OF;
26
 
27
    T sign_bit = 0x80 << (8 * (sizeof(T) - 1));
28
    for (int i = 0; i < count; ++i) {
29
        flags &= ~CF;
30
        if (v & sign_bit)
31
            flags |= CF;
32
        v <<= 1;
33
    }
34
 
35
    if (!v)
36
        flags |= ZF;
37
    if (!__builtin_parity(v & 0xff))
38
        flags |= PF;
39
    if (v & sign_bit)
40
        flags |= SF;
41
 
42
    return std::make_pair(flags, v);
43
}
44
 
45
// shl r/m, N
46
void EmulatorPimpl::shlc0()
47
{
48
    auto v = read_data<uint8_t>();
49
    auto count = fetch_byte();
50
    uint16_t flags;
51
 
52
    if (!(count & 0x1f))
53
        return;
54
 
55
    std::tie(flags, v) = do_shl(v, count & 0x1f);
56
 
57
    write_data<uint8_t>(v);
58
    registers->set_flags(flags, CF | ZF | PF | SF);
59
}
60
 
61
// shl r/m, 1
62
void EmulatorPimpl::shlc1()
63
{
64
    auto v = read_data<uint16_t>();
65
    auto count = fetch_byte();
66
    uint16_t flags;
67
 
68
    if (!(count & 0x1f))
69
        return;
70
 
71
    std::tie(flags, v) = do_shl(v, count & 0x1f);
72
 
73
    write_data<uint16_t>(v);
74
    registers->set_flags(flags, CF | ZF | PF | SF);
75
}
76
 
77
// shl r/m, 1
78
void EmulatorPimpl::shld0()
79
{
80
    auto v = read_data<uint8_t>();
81
    uint16_t flags;
82
 
83
    std::tie(flags, v) = do_shl(v, 1);
84
 
85
    write_data<uint8_t>(v);
86
    registers->set_flags(flags, OF | CF | ZF | PF | SF);
87
}
88
 
89
// shl r/m, 1
90
void EmulatorPimpl::shld1()
91
{
92
    auto v = read_data<uint16_t>();
93
    uint16_t flags;
94
 
95
    std::tie(flags, v) = do_shl(v, 1);
96
 
97
    write_data<uint16_t>(v);
98
    registers->set_flags(flags, CF | OF | ZF | PF | SF);
99
}
100
 
101
// shl r/m, N
102
void EmulatorPimpl::shld2()
103
{
104
    auto v = read_data<uint8_t>();
105
    uint16_t flags;
106
 
107
    if (!registers->get(CL))
108
        return;
109
 
110
    std::tie(flags, v) = do_shl(v, registers->get(CL));
111
 
112
    write_data<uint8_t>(v);
113
    registers->set_flags(flags, CF | ZF | PF | SF);
114
}
115
 
116
// shl r/m, N
117
void EmulatorPimpl::shld3()
118
{
119
    auto v = read_data<uint16_t>();
120
    uint16_t flags;
121
 
122
    if (!registers->get(CL))
123
        return;
124
 
125
    uint16_t result;
126
    std::tie(flags, result) = do_shl(v, registers->get(CL));
127
 
128
    write_data<uint16_t>(result);
129
    registers->set_flags(flags, CF | ZF | PF | SF);
130
}

powered by: WebSVN 2.1.0

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