z80 Development Kit
You are not logged in.
Hello,
I have trouble with usign ANSI on ZX Spectrum (this is working the same way on 1.7 and 1.8 )
#include<stdio.h>
#include<spectrum.h>
/* Set color of inkoust and paper */
void ansi_set_inkpaper(unsigned int inkoust, unsigned int paper )
{
printf ("%c[%u;%um",27,inkoust+30,paper+40);
return;
}
/* Clear screen */
void ansi_clear_screen()
{
printf ("%c2J",155);
return;
}
/* Set position of cursor, begins with row 0, col 1? */
void ansi_set_position(unsigned int row, unsigned int col)
{
printf ("%c[%u;%uH",27,row,col);
return;
}
// no color changes for one character only
void trouble1()
{
zx_border(2); // this is to view where is border (RED)
ansi_clear_screen();
ansi_set_position(0,1); // top and left position (why 0,1?)
ansi_set_inkpaper(1,2); // red on green
printf("%c",'X'); // when print only one character no attribute used
//printf("%c",'X'); // with next character are BOTH colored
}
/* main function */
int main(int argc, char *argv[])
{
trouble1();
return 0;
}zcc +zxansi -lndos -oansitroubles.bin -create-app ansitroubles.c
xspect -quick-load ansitroubles.tap
Important: I am using for now emulators only (xspect, xfuse) - the same efect on both.
Z88DK is compiled on my Ubuntu Linux 6.10 from source codes.
I'm new here so maybe this is not bug but I do some wrong...
Leonell
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Last edited by leonell (2008-03-09 19:12:03)
Offline
Hello,
at a first glance I see two things that are not very good:
- For the output name, avoid forcing the "bin" extension
- don't use the %u format in the ANSI escape sequences: they'd probably give problem with a real VT-100, too.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
Stef, I believe the problem is in libsrc/stdio/ansi/spectrum/f_ansi_char.asm
An excerpt:
ld a,(ansi_COLUMN) ; Column text position ld e,a ld d,0 or d jr z,ZCL .LP add hl,de djnz LP ld b,3 .LDIV srl h rr l rra djnz LDIV ; Added for color handling push hl push af ld de,22528-32 add hl,de ld a,(ansi_ROW) inc a ld de,32 .CLP add hl,de dec a jr nz,CLP ld a,(23693) ;Current color attributes ld (hl),a pop af pop hl ; end of color handling ld b,5 .RGTA srl a djnz RGTA .ZCL ld (PRE+1),a
where you can see if the ansi column is zero the code branches to "ZCL" to avoid the multiplication / division required by variable width font. But "ZCL" also bypasses the colour handling code.
I'm not too familiar with this code so I'll leave it to you to comment!
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
ansi_set_position(0,1); // top and left position (why 0,1?)
This might be a bug then.
http://en.wikipedia.org/wiki/ANSI_escape_code
says cursor coordinates are 1-based which means the top row should also have coordinate 1?
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
says cursor coordinates are 1-based which means the top row should also have coordinate 1?
With ansi_set_position(1,1) is cursor placed (target -zxansi ) to first column of second row (tested Z88DK 1.8 and Z88DK 1.7).
Leonell
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
for the output name, avoid forcing the "bin" extension
Done.
don't use the %u format in the ANSI escape sequences: they'd probably give problem with a real VT-100, too.
And what to use instead? "%d" ?
My code is based on example "z88dk/examples/sam/ansitest.c" from distribution, there are used "%u" ...
Thanks,
Leonell
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
says cursor coordinates are 1-based which means the top row should also have coordinate 1?
With ansi_set_position(1,1) is cursor placed (target -zxansi ) to first column of second row (tested Z88DK 1.8 and Z88DK 1.7).
Yes that's what I'm seeing but I think it should be placed in the first column of the first row. These problems should be fairly easy to fix but I will wait for Stef to comment as he's the man for ansi terminal emulation ![]()
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
Yes that's what I'm seeing but I think it should be placed in the first column of the first row. These problems should be fairly easy to fix but I will wait for Stef to comment as he's the man for ansi terminal emulation :)
OK. I wanted also test it in 32col mode and don't know how to switch it.
In wiki ( http://www.z88dk.org/wiki/doku.php/platform:zx ) is:
The new screen driver is still fairly minimal but it does feature 64 column text and 32 column text (switchable)...
1,64 - Switch to 64 column mode
1,32 - Switch to 32 column mode
...
All the commands, except for the udg address/font address can be embedded in strings using either octal or hexadecimal representation.
But how?
/* Switch display mode to 32 column */
void ansi_width_standard()
{
printf ("%c%c%c",27,1,32);
//printf ("%c%c",1,32);
//printf ("%c1;32",27);
//printf ("%c[1;32",27);
//printf ("%c%c;32",27,1);
//printf ("%c%c32",27,1);
//printf ("%c[1;32m",27,1);
//printf ("%c[%c;%cm",27,1,32);
//printf ("%c[%c;32m",27,1);
return;
}Any of commands above does not switch to 32 columns mode..
Thanks.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
Code:
don't use the %u format in the ANSI escape sequences: they'd probably give problem with a real VT-100, too.And what to use instead? "%d" ?
My code is based on example "z88dk/examples/sam/ansitest.c" from distribution, there are used "%u" ...
I think the point Stef is making is that ansi terminals understand 7-bit ascii only. If the values you use in %u or %d are > 127 or are negative, the ansi terminal might misbehave but that is under your program control. %c might be safer as the compiler can be instructed to use unsigned chars only, which may reset bit 7 but I'm not absolutely sure about that. And if you do this you should probably explicity cast your arguments as unsigned char which is a pain, although it will work without doing this.
I wouldn't worry too much about it -- I would use %u too -- just as long as you're aware the parameter must by >=0 and <=127.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
I think the point Stef is making is that ansi terminals understand 7-bit ascii only. If the values you use in %u or %d are > 127 or are negative, the ansi terminal might misbehave but that is under your program control. %c might be safer as the compiler can be instructed to use unsigned chars only, which may reset bit 7 but I'm not absolutely sure about that. And if you do this you should probably explicity cast your arguments as unsigned char which is a pain, although it will work without doing this.
I wouldn't worry too much about it -- I would use %u too -- just as long as you're aware the parameter must by >=0 and <=127.
Pah, I don't know what I was thinking here... it's not an ascii code that gets written but a number... So %u is just fine. Perhaps Stef was thinking the same ![]()
OK. I wanted also test it in 32col mode and don't know how to switch it.
In wiki ( http://www.z88dk.org/wiki/doku.php/platform:zx ) is:Code:
The new screen driver is still fairly minimal but it does feature 64 column text and 32 column text (switchable)... 1,64 - Switch to 64 column mode 1,32 - Switch to 32 column mode ...All the commands, except for the udg address/font address can be embedded in strings using either octal or hexadecimal representation.
But how?
There are two different spectrum screen output drivers: one is the "regular console driver" which you get when you compile programs with "+zx" and for which the above will apply. The other is the "ansi terminal emulation" which you get when you compile programs with "+zxansi". Only the +zx driver understands those codes above.
The +zxansi ansi terminal emulation does support many different font sizes and a couple of font choices but it only supports one font and font size at a time. The library must be recompiled to choose another font or font size, as detailed at the end of the page you linked and cannot change this at runtime. Much like a real ansi terminal
The default zxansi library supplied does 64 columns as you've seen.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
There are two different spectrum screen output drivers: one is the "regular console driver"
Thanks you very much. With "zx" driver are the sequences working fine.
I rebuilded also the library with ZXCOLS=ROM32 and now I have 32 columns with "zxansi".
But next problem with ANSI (and 32 column):
I set position and color scheme and then print three "X". Character in first column does not have correct color. This is related to first column - with column 5 all is fine.
/* main function */
int main(int argc, char *argv[])
{
zx_border(2); // this is to view where is border (RED)
ansi_clear_screen();
ansi_set_position(0,1); // top and left position
ansi_set_inkpaper(1,2); // red on green
printf("%s","XXX"); // first character is B/W and next two are R/G
ansi_set_position(1,1); // second row
printf("%s","ZZZ"); // the same, 1xB/W and 2x R/G
ansi_set_position(2,5); // not first column
printf("%s","ZZZ"); // correct, 3x R/G
return 0;
}Leonell
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Last edited by leonell (2008-03-10 09:08:52)
Offline
I must apologize.
%u is just ok, as you already noticed ![]()
http://www.z88dk.org/wiki/doku.php/exam … s:ansitest
About the row/column position, you could be right.. I'll put it in the todo list and hopefully I'll learn to re-read the docs (and the code) I wrote, before. Sorry.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
I must be totally burnt, the %u parameter is ok:
http://www.z88dk.org/wiki/doku.php/exam … s:ansitest
I'll dig in the ANSI emulation code and fix the positioning, if necessary (if so, the error could exist in many ports).
Just consider it in my TODO list.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
I must be totally burnt, the %u parameter is ok:
http://www.z88dk.org/wiki/doku.php/exam … s:ansitest
I'll dig in the ANSI emulation code and fix the positioning, if necessary (if so, the error could exist in many ports).
Just consider it in my TODO list.
Yup, now it is fixed.
The cursor positioning command was buggy; fix is valid for many platforms.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
CVS commit complete, bug fixed.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
CVS commit complete, bug fixed.
Hi Stefano,
sorry, not for me :-) Maybe I m doing something wrong but no changes. Still positioning from 0,0 (this is not problem for me) and single char in first column without color attributes.
Detail:
I downloaded last CVS code with:
cvs -d:pserver:anonymous@...:/cvsroot/z88dk login cvs -z3 -d:pserver:anonymous@...:/cvsroot/z88dk co -P z88dk
My code is:
#include<stdio.h>
#include<spectrum.h>
/* Set color of inkoust and paper */
void ansi_set_inkpaper(unsigned int inkoust, unsigned int paper )
{
printf ("%c[%d;%dm",27,inkoust+30,paper+40);
return;
}
/* Clear screen */
void ansi_clear_screen()
{
printf ("%c2J",155);
return;
}
/* Set position of cursor, begins with row 0, col 1? */
void ansi_set_position(unsigned int row, unsigned int col)
{
printf ("%c[%d;%dH",27,row,col);
return;
}
/* main function */
int main(int argc, char *argv[])
{
zx_border(2); // this is to view where is border (RED)
ansi_clear_screen();
ansi_set_position(1,0); // first column of second row
ansi_set_inkpaper(1,3); // red on green
printf("%s","Z"); // without color
return 0;
}Compile:
zcc +zxansi -lndos -oansitroubles -create-app ansitroubles.c
And run with emulator:
fuse --tape ansitroubles.tap
Thanks.
Leonell
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
CVS commit complete, bug fixed.
Hi Stefano,
sorry, not for me :-) Maybe I m doing something wrong but no changes. Still positioning from 0,0 (this is not problem for me) and single char in first column without color attributes.
Yep, I was about to post the same thing. The problem's going to be in stdio/ansi/spectrum/f_ansi_char
If I use (1,1) as the coordinate, which I think is correct for the top left corner of the screen, it draws the char one character right and one character down from the top left corner. The screen address likely has to be computed after the row and col positions have been decreased by one.
If 0 is supposed to be a valid column coordinate for the left edge of the screen, no characters in column 0 will be coloured because of the branch to ZCL which skips the colour code in this code fragment:
ld hl,DOTS+1 ld b,(hl) ld hl,0 ld a,(ansi_COLUMN) ; Column text position ld e,a ld d,0 or d jr z,ZCL
But I think (1,1) is supposed to be the top left corner for ansi terminals? Do ansi terminals do any error checking on out of bounds coordinate values?
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
>But I think (1,1) is supposed to be the top left corner for ansi terminals? Do ansi terminals do any error >checking on out of bounds coordinate values?
-------------------------------------------------------------------------
You are right, I twisted the engine even more, assuming (0;0) as position for the top-left corner.
I can't fix right now, but I'll do it soon.
The colour bug never came up just because of the colour-clash.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
The colour bug never came up just because of the colour-clash.
What colour-clash do you mean ? If you mean that 2 characters in 64 cols mode share the same attribute then OK.
But it does not work in 32 column mode too.
Leonell
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
>What colour-clash do you mean ? If you mean that 2 characters in 64 cols mode share the same attribute then OK.
>But it does not work in 32 column mode too.
The VT Emulation on the Spectrum can be compiled for many text resolutions.
In 24, 28 and 32 columns mode the color-clash doesn't hide the problem, but starting from 36 columns, it will happen.
see: http://www.z88dk.org/wiki/doku.php/plat … ole_driver
I've fixed (hopefully) the color problem, and the cursor positioning code is a little more clean, but oddly I couldn't find a safe way to force the row and columns boundary for zero and negative values, so I just gave up for now.. be careful to respect the internal screen range, and forgive the fuzziness.
Hope to find a decent fix soon (it mustn't be difficult but... grr).
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
I've fixed (hopefully) the color problem, and the cursor positioning code is a little more clean
Thank you very much, my example now is working.
Leonell
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline
> Thank you very much, my example now is working.
>
> Leonell
You're welcome, the VT range protection should be ok, now.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Offline